对于每个蚂蚁来说,他相对于其他蚂蚁的位置是固定的,赶超前面或者被后面的赶超都是要回头的,所以,假设木棒无限长,位于第一位的永远在第一位,第二位的永远在第二位。同时每两个碰头的蚂蚁其实就是类似于交换了身份继续前进。所以,先不管他们的碰头,就让每个沿着路线继续走下去。由前面的可以知道,每个蚂蚁的最终状态的位置排列的顺序就是一开始蚂蚁的编号。
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
//typedef __int64 Int;
typedef pair<int, int> PAI;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 10000 + 10;
struct ant {
int dic;
int s, e;
int id;
} data[MAXN];
int f[MAXN];
char str[][10] = {"L", "Turning", "R", "Fell off"};
bool cmp(ant x, ant y) {return x.e < y.e;}
bool cmp1(ant x, ant y) {return x.s < y.s;}
int main() {
int T;
char s[10];
scanf("%d", &T);
int Kcase = 0;
while (T--) {
int L, t, N;
scanf("%d%d%d", &L, &t, &N);
for (int i = 0; i < N; i++) {
scanf("%d%s", &data[i].s, s);
data[i].dic = (s[0] == 'R'? 1: -1);
data[i].id = i;
data[i].e = data[i].s + data[i].dic*t;
}
sort(data, data + N, cmp1);
for (int i = 0; i < N; i++) f[data[i].id] = i;
sort(data, data + N, cmp);
for (int i = 0; i < N; i++) {
if (data[i].e < 0 || data[i].e > L) data[i].dic = 2;
else if (i != 0 && data[i].e == data[i - 1].e) data[i].dic = data[i - 1].dic = 0;
}
printf("Case #%d:\n", ++Kcase);
for (int i = 0; i < N; i++) {
int u = f[i];
int v = data[u].dic;
if (v == 2) printf("Fell off\n");
else printf("%d %s\n", data[u].e, str[v+1]);
}
printf("\n");
}
return 0;
}