"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." |
Piotr likes playing with ants. He has n of them on a horizontalpole L cm long. Each ant is facing either left or right and walksat a constant speed of 1 cm/s. When two ants bump into each other, theyboth turn around (instantaneously) and start walking in opposite directions.Piotr knows where each of the ants starts and which direction it is facingand wants to calculate where the ants will end up T seconds from now.
Input
The first line of input gives the number of cases, N. Ntest cases follow. Each one starts with a line containing 3 integers:L ,
T and n
Output
For each test case, output one line containing "Case #x:"followed by
n lines describing the locations and directions of then ants in the same format and order as in the input. If two or moreants are at the same location, print "Turning" instead of "L" or "R" fortheir 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.
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 |
关键在于理解存在的映射关系。
#include<iostream> #include<string> #include<set> #include<queue> #include<vector> #include<map> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> using namespace std; #define Max(a,b) (a>b?a:b) #define Min(a,b) (a<b?a:b) #define Mem(a,b) (memset(a,b,sizeof(a))) #define N 10005 typedef long long ll; struct node { int num;//输入顺序 int p;//位置 int from;//方向 }; node sta[N],ed[N]; int ord[N]; bool cmp(node a,node b) { return a.p<b.p; } char to[][10] = {"L", "Turning", "R"}; int main() { int T; int n,m; int cnt= 1; int i,j,k; char s[2]; int a,b,c; scanf( "%d", &T ); while( T-- ) { scanf( "%d%d%d", &n, &m, &k ); for( i= 0; i< k ; i++ ) { scanf( "%d%s", &a, s ); b= ( s[0] == ‘L‘? -1: 1 ); sta[i].num= i, sta[i].p = a;sta[i].from= b; ed[i].num = 0;ed[i].p = a+ m*b;ed[i].from= b; } sort(sta, sta+k, cmp); for( i= 0; i< k ; i++ ) ord[sta[i].num] = i; sort(ed,ed+k, cmp); for( i= 0; i< k ; i++ ) if(ed[i].p == ed[i+1].p) ed[i].from = ed[i+1].from = 0; printf("Case #%d:\n",cnt++); for( i= 0; i< k; i++ ) { a= ord[i]; if( ed[a].p <0 ||ed[a].p> n ) printf("Fell off\n"); else printf("%d %s\n",ed[a].p, to[ed[a].from+1]); } printf("\n"); } return 0; }
原文:http://blog.csdn.net/jingshui1234/article/details/24270259