zoukankan      html  css  js  c++  java
  • 10881

    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 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<algorithm>
    using namespace std;
    struct data{int x;char u;int o;}p[10005];
    int temp(void const *p,void const *q)
    {
    	return (*(data *)p).x-(*(data *)q).x;
    }
    
    int main()
    {
    	int N,l,t,n,count=1;
    	cin>>N;
    	while(N--)
    	{
    		int order[10005]={0};
    		cin>>l>>t>>n;
    		for(int i=0;i<n;i++)
    		{
    			cin>>p[i].x>>p[i].u;
    			p[i].o=i;
    		}
    		qsort(p,n,sizeof(p[0]),temp);
    		for(int i=0;i<n;i++)
    			order[p[i].o]=i;
    		for(int i=0;i<n;i++)
    			if(p[i].u=='L') p[i].x-=t;
    			else p[i].x+=t;
    			qsort(p,n,sizeof(p[0]),temp);
    			for(int i=0;i<n-1;i++)
    				if(p[i].x==p[i+1].x)
    				{
    					p[i].u='T';
    					p[i+1].u='T';
    				}
    				cout<<"Case #"<<count++<<":"<<endl;
    				for(int i=0;i<n;i++)
    				{
    					int a=order[i];
    					if(p[a].x<0||p[a].x>l) cout<<"Fell off"<<endl;
    					else 
    					{
    						cout<<p[a].x<<" ";
    						if(p[a].u=='T') cout<<"Turning"<<endl;
    						else cout<<p[a].u<<endl;
    					}
    				}
    				cout<<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    【转载】python字符格式化
    【python】生产者消费者问题优化
    【python】pickle文件读写的insecure问题
    【工具使用】sublime设置换行符为unix风格
    【python】CGI部署问题解决
    【python】引用c的dll
    【多线程】Python进程,队列和锁相关的一些问题
    【Python】下载图片,标识之后转发出去
    【Python】PIL在window64位机制上引用异常问题解决
    【Python网页分析】httplib库的重定向处理
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3141321.html
Copyright © 2011-2022 走看看