UVA 10881 Piotr's Ants

来源:互联网 发布:pdf.js 获取当前页数 编辑:程序博客网 时间:2024/05/17 03:05


#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

Download as PDF

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 beforeT seconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample InputSample Output
210 1 41 R5 R3 L10 R10 2 34 R5 L8 R
Case #1:2 Turning6 R2 TurningFell offCase #2:3 L6 R10 R

0 0
原创粉丝点击