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;
    }


  • 相关阅读:
    Hibernate 配置双向多对多关联
    转 方法区(method) )、栈区(stack)和堆区(heap)之JVM 内存初学
    java web 实战经典(二)
    flex 生成多边形时内、外环计算
    java web 开发实战经典(一)
    sql语句联表更新(从一个数据库中的一张表更新到另一个数据库的另一张表)
    javascript DOM编程艺术(检测与性能优化)
    java数据库基本操作(sqlserver 2000为例)
    同一台电脑上配置多个解压版tomcat方法(本例安装两个)
    css那些事(一)
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3141321.html
Copyright © 2011-2022 走看看