\One thing is for certain: there is no stopping them; the ants will
soon be here. And I, for one, welcome our new insect overlords.” Kent
Brockman Piotr likes playing with ants. He has n of them on a
horizontal pole L cm long. Each ant is facing either left or right and
walks at a constant speed of 1 cm/s. When two ants bump into each
other, they both turn around (instantaneously) and start walking in
opposite directions. Piotr knows where each of the ants starts and
which direction it is facing and wants to calculate where the ants
will end up T seconds from now. Input The rst line of input gives the
number of cases, N . N test cases follow. Each one starts with a line
containing 3 integers: L , T and n (0 n 10000). The next n lines
give the locations of the n ants (measured in cm from the left end of
the pole) and the direction they are facing ( L or R ). Output For
each test case, output one line containingCase # x : ' followed by
Turning ’ instead of
n lines describing the locations and directions of the n ants in the
same format and order as in the input. If two or more ants are at the
same location, printL ' or
R ’ for their
direction. If an ant falls off the pole before T seconds, print ` Fell
off ’ for that ant. Print an empty line after each test case.
结论1:两个蚂蚁相碰掉头,和直接相背而行,在考虑n个蚂蚁的位置的时候是等价的。
但是,我们还要知道这些位置哪个属于哪个蚂蚁。
结论2:蚂蚁之间的相对位置【谁在谁左右】不变。
先处理出每个蚂蚁在第几个位置,然后对末位置排序就知道谁是谁了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct ant
{
int p,d,num;
/*-1:left 1:right*/
bool operator < (const ant & aa) const
{
if (p!=aa.p) return p<aa.p;
return d<aa.d;
}
}a[10010];
int ord[10010],n,l,t;
int main()
{
int T,i,x,y,K;
char c[3];
scanf("%d",&T);
for (K=1;K<=T;K++)
{
scanf("%d%d%d",&l,&t,&n);
for (i=1;i<=n;i++)
{
scanf("%d%s",&a[i].p,c);
if (c[0]=='L') a[i].d=-1;
else a[i].d=1;
a[i].num=i;
}
sort(a+1,a+n+1);
for (i=1;i<=n;i++)
ord[a[i].num]=i;
for (i=1;i<=n;i++)
if (a[i].d==1)
a[i].p=min(l+1,a[i].p+t);
else
a[i].p=max(-1,a[i].p-t);
sort(a+1,a+n+1);
printf("Case #%d:\n",K);
for (i=1;i<=n;i++)
{
x=a[ord[i]].p;
y=a[ord[i]].d;
if (x==-1||x==l+1) printf("Fell off\n");
else
{
printf("%d ",x);
if ((ord[i]>1&&x==a[ord[i]-1].p)||(ord[i]<n&&x==a[ord[i]+1].p))
printf("Turning\n");
else
printf("%s",y==1?"R\n":"L\n");
}
}
printf("\n");
}
}