分析:
蚂蚁相撞的时候位置可以看做互相通过。
所有蚂蚁的相对位置不变。
初始位置排序,计算出T时间之后初始位置处于的位置和运动方向。
根据原来的相对位置套进去,位置和方向皆得到。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define pr(x) cout << #x << ": " << x << " "
#define pl(x) cout << #x << ": " << x << endl;
const int maxN = 1e5+10;
struct ant {
int id;//表示该蚂蚁从左至右是第几只蚂蚁
int pos;//记录该蚂蚁当前坐标位置
int dir;//记录该蚂蚁的方向,left表示-1,right表示1,turning表示0,。
};
ant before[maxN], after[maxN];
int inds[maxN];
bool cmp(const ant &a, const ant &b)
{
return a.pos < b.pos;
}
int main()
{
int icase, l, t, n, w = 1;
scanf("%d", &icase);
while(icase--)
{
scanf("%d%d%d", &l, &t, &n);//l为杆子长度,t表示输出t秒后的状态,n表示数据个数
for(int i = 0; i < n; ++i)
{
int p, d;
char c;
scanf("%d %c", &p, &c);
d = ('L'==c ? -1 : 1);
before[i] = (ant){i, p, d};
after[i] = (ant){0, p+t*d, d};
}
sort(before, before+n, cmp);
for(int i = 0; i < n; ++i)
inds[before[i].id] = i;
sort(after, after+n, cmp);
for(int i = 0; i < n-1; ++i)
{
if(after[i].pos == after[i+1].pos)
after[i].dir = after[i+1].dir = 0;
}
printf("Case #%d:\n", w++);
for(int i = 0; i < n; ++i)
{
if(after[inds[i]].pos>l ||after[inds[i]].pos<0)
printf("Fell off\n");
else if(0 == after[inds[i]].dir)
printf("%d Turning\n", after[inds[i]].pos);
else if(1 == after[inds[i]].dir)
printf("%d R\n", after[inds[i]].pos);
else if(-1 == after[inds[i]].dir)
printf("%d L\n", after[inds[i]].pos);
}
printf("\n");
}
return 0;
}