题目链接:https://vjudge.net/problem/UVA-10881
题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向。
并且当相邻两个蚂蚁相撞时转向。现在问t时间后各个蚂蚁的位置。
这题的一个技巧就是把两只蚂蚁的相撞看作是两只蚂蚁交换穿过对方并且交换蚂蚁的编号。
最主要是要注意到
第一遍排序后 得到按位置升序排序的蚂蚁位置
不论之后这些蚂蚁怎么爬 他们的相对位置都是不变的啊!!!!
就是第1只蚂蚁一直都是在第1位 第2只一直都在第2位...!!!!
所以对最终全部蚂蚁的位置排序
题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向。
并且当相邻两个蚂蚁相撞时转向。现在问t时间后各个蚂蚁的位置。
这题的一个技巧就是把两只蚂蚁的相撞看作是两只蚂蚁交换穿过对方并且交换蚂蚁的编号。
最主要是要注意到
第一遍排序后 得到按位置升序排序的蚂蚁位置
不论之后这些蚂蚁怎么爬 他们的相对位置都是不变的啊!!!!
就是第1只蚂蚁一直都是在第1位 第2只一直都在第2位...!!!!
所以对最终全部蚂蚁的位置排序
第1个位置就是第1只蚂蚁的 第2个位置就是第2只蚂蚁的....
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;
struct Ant{
int pos, dir, id;
bool operator < (const Ant& a) const{
return pos < a.pos;
}
}ant[N];
int order[N];
int main(void)
{
// freopen("out.txt", "w", stdout);
int T, len, t, n, cas = 1;
string s;
cin >> T;
while (T--)
{
cin >> len >> t >> n;
for (int i = 1; i <= n; i++){
cin >> ant[i].pos >> s;
if (s[0] == 'R') ant[i].dir = 1;
else ant[i].dir = -1;
ant[i].id = i;
}
sort(ant+1, ant+1+n);
for (int i = 1; i <= n; i++){
order[ant[i].id] = i; // 记录输入顺序和蚂蚁相对顺序的一个映射
ant[i].pos += t * ant[i].dir;
}
sort(ant+1, ant+1+n);
ant[0].pos = ant[n+1].pos = -1;
printf("Case #%d:\n", cas++);
for (int i = 1; i <= n; i++){
int p = order[i];
if (ant[p].pos < 0 || ant[p].pos > len)// 位置可以等于0。。。。
puts("Fell off");
else if (ant[p-1].pos == ant[p].pos || ant[p+1].pos == ant[p].pos)
printf("%d Turning\n", ant[p].pos);
else
printf("%d %c\n", ant[p].pos, ant[p].dir==1?'R':'L');
}
puts("");
}
return 0;
}