UVA 10881 Piotr's Ants
2014-12-28 12:32
435 查看
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <climits> #include <cstdlib> #include <ctime> using namespace std; //ios::sync_with_stdio(false); struct node { int x,c,i; }; node a[10005]; node next[100005]; int cmp(node a,node b) { return a.x < b.x; } int cmp1(node a,node b) { return a.i < b.i; } int main() { ios::sync_with_stdio(false); int q; cin >> q; for(int h = 1; h <= q ;h++) { int l,t,n; cin >> l >> t >> n; for(int i = 0; i < n ;i++) { char ch; cin >> a[i].x >> ch; a[i].c = (ch == 'R') ? 1: -1; next[i].x = a[i].x + t * a[i].c; next[i].c = a[i].c; a[i].i = i; } sort(a,a+n,cmp); sort(next,next+n,cmp); for(int i = 0 ; i < n;i++) { next[i].i = a[i].i; if(i < n-1 && next[i].x == next[i+1].x) { next[i].c = 0; next[i+1].c = 0; } } sort(next,next+n,cmp1); printf("Case #%d:\n",h); for(int i = 0; i < n;i++) { if(next[i].x > l||next[i].x < 0) { printf("Fell off\n"); } else { printf("%d ",next[i].x); if(next[i].c == 0 ) { printf("Turning\n"); } else if(next[i].c == 1) { printf("R\n"); } else { printf("L\n"); } } } printf("\n"); } return 0; }注意到每一只蚂蚁碰到之后都转向,就相当于两只蚂蚁没有转头一样仍然按照原来的方向前进。如果按照这种方法求的话那么t时间之后在什么位置有蚂蚁就可以求出来了。
但那时这时候每只蚂蚁的方向确定了,但是这只蚂蚁是原来的什么位置的蚂蚁却不知道。通过观察可以知道。蚂蚁之间的相对位置从来没有改变,也就是说每只蚂蚁旁边的蚂蚁是哪只蚂蚁没有改变,如果把原先的蚂蚁按照他们在绳子上的位置排序,那么t时间之后的蚂蚁的位置的排序也是对应的蚂蚁就是同一只蚂蚁。
Description
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." |
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, 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 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 beforeT 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 |
相关文章推荐
- uva 10881 - Piotr's Ants
- uva 10881 Piotr's Ants (模拟)
- Uva 10881 Piotr's Ants (模拟+思维)
- UVA_10881 - Piotr's Ants
- UVA - 10881 Piotr's Ants
- UVA - 10881 - Piotr's Ants
- UVA 10881 Piotr's Ants
- UVA 10881 Piotr's Ants
- UVa 10881 Piotr's Ants
- UVA-10881 Piotr's Ants (想法题)
- UVa 10881 - Piotr's Ants
- 蚂蚁-UVA 10881 - Piotr's Ants
- uva_10881_Piotr's Ants (模拟,Sort)
- UVa 10881 Piotr's Ants (等价转化思想)
- Uva-10881-Piotr's Ants
- 例题5 UVA 10881 蚂蚁(Piotr's Ants)
- UVa 10881 Piotr's Ants 蚂蚁
- UVA - 10881 Piotr's Ants 推理
- UVA - 10881 Piotr's Ants
- uva10881 - Piotr's Ants (等效变换)