首页 > 其他 > 详细

uva 10881

时间:2014-04-21 23:55:10      阅读:694      评论:0      收藏:0      [点我收藏+]
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 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 (0 <= n <= 10000).The next n lines give the locations of the n ants (measuredin 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 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;
}


 


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

uva 10881,布布扣,bubuko.com

uva 10881

原文:http://blog.csdn.net/jingshui1234/article/details/24270259

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!