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


  • 相关阅读:
    JS给数字加千位分隔符
    前端防抖与节流实现与应用
    JS实现单向链表、双向链表、循环链表
    单点登录
    AMD、CMD规范
    JS实现全排列
    event loop、进程和线程、任务队列
    BOM属性对象方法
    JS的闭包、高阶函数、柯里化
    for...in、for...of、forEach()有什么区别
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3141321.html
Copyright © 2011-2022 走看看