(luogu1007)独木桥

虽然不难但要点技巧

思路:

可将独木桥看成一条线段, 士兵是线段上的点

由于士兵的移动速度一样, 所以士兵之间没有差别

因此, 当两个士兵相遇时转身时, 可以当作他们穿过了对方

所以只需要统计每个士兵与桥头的距离即可

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int Mn(5050);
int main()
{
int l,n;
scanf("%d%d",&l,&n);
int a1(0),a2(0);
for(int i(1);i<=n;++i)
{
int x;
scanf("%d",&x);
a1 = max(a1,min(x,l-x+1));
a2 = max(a2,max(x,l-x+1));
}
printf("%d %d",a1,a2);
return 0;
}