http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=26579
Andrew the Ant
Andrew the Ant is fascinated by the behavior of his friends. Thousands of them are marching their paths on and on. They can build highly organized ant-hills. Sometimes, however, they act a little bit stupidly.
Recently, Andrew watched his fellow ants marching on top of a long piece of wood. He noticed their behavioral pattern is very simple: Each ant walks slowly forward with a constant speed of 1 cm per second. Whenever it meets another ant, both of them only touch with their antennae and immediately turn around and walk the opposite direction. If an ant comes to the end of the wood, it falls down and does not affect other ants anymore.
The picture above shows an example of moving ants in time 0 s. In one second, the ants E and A meet at position 2 and change their directions. The ant A then meets B in the next 1.5 seconds. At the same time (2.5 seconds after the start), the ants C and D will meet too. All four of them change their directions. In the next 0.5 second (time 3 s), the first ant (E) falls down off the left end, etc.
Your task is to simulate the movement of ants. For simplicity, suppose that the ants have zero size (although the picture could suggest something else).
Input
The input consists of several scenarios. Each scenario starts with a line containing two integer numbers L and A, separated by a space. L is the length of the wood in cms (1 ≤ L ≤ 99 999), and A is the number of ants at the beginning of the simulation (1 ≤ A ≤ L + 1).
Then there are A lines, each containing a positive integer Xi, one space, and an uppercase letter. The number (0 ≤ Xi ≤ L) specifies the position of the i-th ant and the letter its initial direction: either “L” for left (towards zero) or “R” for right. No two ants will start at the same position.
Output
For each scenario, you should print a single line containing the text “The last ant will fall down in T seconds - started at P.”, where T is the exact time when the last ant (or two) reaches the end of the wood, and P is the position where that particular ant has originally started in time 0. If two last ants fall down at the same time, print “started at P and Q”, indicating both of their positions, P <Q.
Sample Input
90000 1 0 R 10 1 0 L 14 5 3 L 6 L 13 L 8 R 1 R
Sample Output
The last ant will fall down in 90000 seconds - started at 0. The last ant will fall down in 0 seconds - started at 0. The last ant will fall down in 13 seconds - started at 6 and 8.
Hint
(The last sample input scenario corresponds to the picture.)
Source
题意:给出a个蚂蚁的位置和其正在行走的方向,若相碰则都转向,若到边缘则掉下去,每秒钟走一格,
问最后一只(或两只)蚂蚁是什么时候掉下去的,其初始位置在什么地方;
思路:(参考同学的 )
(以刘汝佳的某题的解题思路为背景)
蚂蚁看作质点,如图,A左走,E右走,显然A和E碰后E左走,而E的左走就相当于A向左走的一个延续,同理A相当于E的延续,这样,n秒后各个蚂蚁的位置就相当于每个蚂蚁不转向走n秒后的位置,但这时仅仅只是所有位置确定了,但该位置我蚂蚁并不确定;又由于相碰后转向,实际上每个蚂蚁的相对顺序没变,如图,五只蚂蚁,初始位置分别为1 3 6 8 13,则3秒后五只蚂蚁的位置为4 0 3 5 10(即 0 3 4 5 10),最后要确定这五个位置分别是哪几个蚂蚁时,就根据原来的相对顺序就知道E在0,A在3,B在4,D在5,C在10;若要找最后一个掉下去的时间,则要找到最右边那个向左走的蚂蚁和最左边那个向右走的蚂蚁,比较一下就行了;
代码是我的
#include<stdio.h>
#include<algorithm>
using namespace std;
struct haha
{
int s;
char flag;
}ant[100000];
int aa[100000];
int cmp(struct haha a,struct haha b)
{
return a.s>b.s;
}
int main()
{
int i,len,n,time,left,right,cnt,n1,n2;
while(scanf("%d %d",&len,&n)!=EOF)
{
right=len;
left=0;
for(i=0;i<n;i++)
{
scanf("%d %c",&ant[i].s,&ant[i].flag);
}
sort(ant,ant+n,cmp);
for(i=n-1;i>=0;i--)
if(ant[i].flag=='R')//向右跑
{
right=ant[i].s;
break;
}
for(i=0;i<n;i++)
if(ant[i].flag=='L')//向左跑
{
left=ant[i].s;
break;
}
time=len-right>left?len-right:left;
for(i=0;i<n;i++)
if(ant[i].flag=='R')
aa[i]=ant[i].s+time;
else aa[i]=ant[i].s-time;
sort(aa,aa+n);
cnt=0;
for(i=0;i<n;i++)
{
if(aa[i]==0||aa[i]==len)
{
cnt++;
if(cnt==1) n1=ant[n-1-i].s;
if(cnt==2) n2=ant[n-1-i].s;
}
}
if(cnt==1)
printf("The last ant will fall down in %d seconds - started at %d.\n",time,n1);
else printf("The last ant will fall down in %d seconds - started at %d and %d.\n",time,n1,n2);
}
return 0;
}