UVa

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25979

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
/***************************************************************************************************************
        题意:一根长L厘米的木棍上有n只蚂蚁,每只蚂蚁有个开始的位置和爬行方向,速度为1.
              当两只蚂蚁相撞后,两者同时掉头继续爬行,求按输入顺序给出每只蚂蚁T秒后的位置后朝向。
        思路:

            1.每只蚂蚁相撞后同时掉头可以看做对穿而过,关键的问题就在于求位置的变化。
            2.按位置从小到大排序,可以发现排序后(befor数组和after数组)所有的蚂蚁相对位置并没有变化,
             改变的只是朝向。

        PS: 重点花时间想了一下次序的问题,order[id] = i,表示第 id 个输入的蚂蚁经排序后的位置为 i
***************************************************************************************************************/

struct Node
{
	int id;
	int p;
	int move;
}node1[10000+5],node2[10000+5];
int order[10000+5];
string s[3] = {"L","Turning","R"};

bool cmp(Node x,Node y){
    return x.p < y.p;
}
int main()
{
	int T,cnt = 0;
	cin>>T;
	while(T--){
		int l,t,n;
		cin>>l>>t>>n;

		int temp,d;
		char c;

		for(int i = 1;i <=n;i ++){
			scanf("%d %c",&temp,&c);
			d = (c == 'L' ? -1 : 1);
			node1[i] = (Node) {i,temp,d};
			node2[i] = (Node) {0,temp + t*d,d};
		}
		sort(node1+1,node1+n+1,cmp);
		for(int i = 1;i <= n;i ++)
			order[node1[i].id] = i;

		sort(node2+1,node2+n+1,cmp);
		for(int i = 1;i < n;i ++)
			if(node2[i].p == node2[i+1].p)
				node2[i].move = node2[i+1].move = 0;
        cout<<"Case #"<<++cnt<<":"<<endl;
		for(int i = 1; i <= n;i ++){
			int a = order[i];
			if(node2[a].p < 0 || node2[a].p > l)	cout<<"Fell off"<<endl;
			else
				cout<<node2[a].p<<" "<<s[node2[a].move+1]<<endl;
		}
		cout<<endl;
	}
	return 0;
}



原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351956.html