UVA10881 - Piotr's Ants

Problem D
Piotr's Ants
Time Limit: 2 seconds

 

"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 first line of input gives the number of cases, NN 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 containing "Case #x:" followed by 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, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole before Tseconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input Sample Output
2

10 1 4

1 R

5 R

3 L

10 R

10 2 3

4 R

5 L

8 R

Case #1:

2 Turning

6 R

2 Turning

Fell off



Case #2:

3 L

6 R

10 R



 


Problemsetter: Igor Naverniouk
Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev

题目大意:在一条长为L的木板上有N只蚂蚁,每只要么向左爬,要么向右爬,速度为1cm/s,当两只蚂蚁相撞时,两者同时掉头。给出蚂蚁的初始位置,请计算T秒之后的N只蚂蚁的位置。

题解:《训练指南的》例题,汝佳大神的分析好巧妙!!!真心想不到耶。

View Code
 1 #include<stdio.h>

 2 #include<string.h>

 3 #define MAXSN 10005

 4 typedef struct

 5 {

 6     long pst;

 7     long dr;

 8     long id;

 9 } NODE;

10 NODE s[MAXSN],xt[MAXSN];

11 long order[MAXSN];

12 char name[][10]={"L","Turning","R"};

13 void qsort(NODE s[],long l,long r)

14 {

15     long i,j,mid;

16     NODE temp;

17     i=l;

18     j=r;

19     mid=s[(l+r)>>1].pst;

20     while(i<=j)

21     {

22         while(s[i].pst<mid) i++;

23         while(s[j].pst>mid) j--;

24         if(i<=j)

25         {

26             temp=s[i];

27             s[i]=s[j];

28             s[j]=temp;

29             i++;

30             j--;

31         }

32     }

33     if(i<r) qsort(s,i,r);

34     if(j>l) qsort(s,l,j);

35 }

36 

37 int main(void)

38 {

39     long i,n,t,tt,l,p;

40     scanf("%ld",&t);

41     p=1;

42     while(t--)

43     {

44         char ch;

45         scanf("%ld%ld%ld",&l,&tt,&n);

46         for(i=0; i<n; i++)

47         {

48             scanf("%ld %c",&s[i].pst,&ch);

49             if(ch=='L')

50             s[i].dr=-1;

51             else

52             s[i].dr=1;

53             s[i].id=i;

54             xt[i]=s[i];

55             xt[i].pst=s[i].pst+tt*xt[i].dr;

56         }

57         qsort(s,0,n-1);

58         for(i=0;i<n;i++)

59         order[s[i].id]=i;

60         qsort(xt,0,n-1);

61         printf("Case #%ld:\n",p++);

62         for(i=0;i<n-1;i++)

63         if(xt[i].pst==xt[i+1].pst) xt[i].dr=xt[i+1].dr=0;

64         for(i=0;i<n;i++)

65         {

66             int a=order[i];

67             if(xt[a].pst<0||xt[a].pst>l) printf("Fell off\n");

68             else

69             printf("%ld %s\n",xt[a].pst,name[xt[a].dr+1]);

70         }

71         printf("\n");

72     }

73     return 0;

74 }

 

你可能感兴趣的:(ant)