洛谷千题详解 | P1007 独木桥【C++、Pascal语言】

博主主页:Yu·仙笙

专栏地址:洛谷千题详解

目录

题目背景

题目描述

输入格式

输出格式

输入输出样例

解析:

 C++源码:

Pascal源码:


 ------------------------------------------------------------------------------------------------------------------------------- 

 ------------------------------------------------------------------------------------------------------------------------------- 

题目背景

战争已经进入到紧要时间。你是运输小队长,正在率领运输部队向前线运送物资。运输任务像做题一样的无聊。你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在桥下欣赏士兵们。士兵们十分愤怒,因为这座独木桥十分狭窄,只能容纳 1 个人通过。假如有 2 个人相向而行在桥上相遇,那么他们 2 个人将无法绕过对方,只能有 1 个人回头下桥,让另一个人先通过。但是,可以有多个人同时呆在同一个位置。

 ------------------------------------------------------------------------------------------------------------------------------- 

题目描述

突然,你收到从指挥部发来的信息,敌军的轰炸机正朝着你所在的独木桥飞来!为了安全,你的部队必须撤下独木桥。独木桥的长度为 L,士兵们只能呆在坐标为整数的地方。所有士兵的速度都为 1,但一个士兵某一时刻来到了坐标为 0 或 L+1 的位置,他就离开了独木桥。

每个士兵都有一个初始面对的方向,他们会以匀速朝着这个方向行走,中途不会自己改变方向。但是,如果两个士兵面对面相遇,他们无法彼此通过对方,于是就分别转身,继续行走。转身不需要任何的时间。

由于先前的愤怒,你已不能控制你的士兵。甚至,你连每个士兵初始面对的方向都不知道。因此,你想要知道你的部队最少需要多少时间就可能全部撤离独木桥。另外,总部也在安排阻拦敌人的进攻,因此你还需要知道你的部队最多需要多少时间才能全部撤离独木桥。

 ------------------------------------------------------------------------------------------------------------------------------- 

输入格式

第一行共一个整数 L,表示独木桥的长度。桥上的坐标为1,2,⋯,L。

第二行共一个整数 N,表示初始时留在桥上的士兵数目。

第三行共有 N 个整数,分别表示每个士兵的初始坐标。

 ------------------------------------------------------------------------------------------------------------------------------- 

输出格式

共一行,输出 2 个整数,分别表示部队撤离独木桥的最小时间和最大时间。2 个整数由一个空格符分开。

 ------------------------------------------------------------------------------------------------------------------------------- 

输入输出样例

输入 #1复制

4
2
1 3

输出 #1复制

2 4

 ------------------------------------------------------------------------------------------------------------------------------- 

解析:

最小时间是第1号士兵向左,第2号士兵向右,结果为2.

最大时间是第1号士兵向右,第2号士兵向左,结果为4.

因此设桥长度为L,士兵方向为D,士兵位置为X,

如果求最小时间,若X<=L/2则D为左,若X>L/2则D为右,最大时间正好与之相反.

我先放出代码,暴力模拟能过就行,复杂度就不说了...

 ------------------------------------------------------------------------------------------------------------------------------- 

 C++源码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <vector>
using namespace std;
namespace Ciyang {
    struct q_instream {
        template < typename classT >
        inline q_instream operator>>(classT &e) const {
            e= 0;
            classT f= 1, c= 0;
            while(c < '0' || c > '9') {
                if(c == '-') f= -1;
                c= getchar();
            }
            while(c >= '0' && c <= '9') e= e * 10 + c - '0', c= getchar();
            return e= e * f, (*this);
        }
    } qin;
    //读入
    struct q_outstream {
        template < typename classT >
        inline q_outstream operator<<(const classT &e) const {
            if(e < 0) {
                putchar('-');
                (*this) << -e;
            }
            else {
                if(e > 9) (*this) << (e / 10);
                putchar(e % 10 + '0');
            }
            return (*this);
        }
        inline q_outstream operator<<(const char &c) const {
            return putchar(c), (*this);
        }
    } qout;
    //输出
}  // namespace Ciyang
using namespace Ciyang;
//这之前都是快输入输出
int n, m, tmpx, tmpy, ans1, ans2;
struct player {
    int x, dir;
};
vector< player > players, rplayers;
//使用vector保存还在桥上的士兵,(名字为player是因为做游戏后遗症)
//players记录最大时间,rplayers记录最小时间
int main() {
    qin >> n >> m;
    for(int i= 1; i <= m; i++) {
        qin >> tmpx;
        if(tmpx <= n / 2)
            tmpy= -1;
        else
            tmpy= 1;
            //位置决定初始方向
        rplayers.push_back((player){tmpx, tmpy});
        players.push_back((player){tmpx, -tmpy});
    }
    //我先求最小时间再求最大时间
    while(!rplayers.empty()) {
    //桥上有士兵就一直循环
        ++ans1;
        for(int i= 0; i < (int)rplayers.size(); i++) {
            rplayers[i].x= rplayers[i].x + rplayers[i].dir;
        }
        //模拟士兵移动
        for(int i= 0; i < (int)rplayers.size(); i++) {
            if(rplayers[i].x == 0 || rplayers[i].x == n + 1) rplayers.erase(rplayers.begin() + i), i--;
        }
        //删除vector中不在桥上的士兵
        //vector删除元素后,原本i下标的士兵变成i-1
        //又因为每次循环i++所以删除后i--
    }
    while(!players.empty()) {
        ++ans2;
        for(int i= 0; i < (int)players.size(); i++) {
            players[i].x= players[i].x + players[i].dir;
        }
        //同理,模拟移动
        for(int i= 0; i < (int)players.size(); i++) {
            if(players[i].x == 0 || players[i].x == n + 1) players.erase(players.begin() + i), i--;
        }
    }
    qout << ans1 << ' ' << ans2 << '\n';
    return 0;
}

 ------------------------------------------------------------------------------------------------------------------------------- 

Pascal源码:

var
  n,m,i,j,len,maxmin,maxmax,m1:longint;
  a:array[1..100000] of longint;
//maxmin、maxmax是最小的最大和最大最大
function min(a,b:longint):longint;
begin
  if a<b then exit(a);
  exit(b);
end;
function max(a,b:longint):longint;
begin
  if a>b then exit(a);
  exit(b);
end;
begin
  readln(len);
  readln(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
  begin
    m:=min(a[i],len+1-a[i]);
    if m>maxmin then
      maxmin:=m;
  end;
  for i:=1 to n do
  begin
    m1:=max(a[i],len+1-a[i]);//都不做解释,题解说明有
    if m1>maxmax then
      maxmax:=m1;
  end;
  writeln(maxmin,' ',maxmax);
end.

------------------------------------------------------------------------------------------------------------------------------- 

猜你喜欢

转载自blog.csdn.net/djfihhfs/article/details/127655538