題目連結
對於每個螞蟻來說,他相對於其他螞蟻的位置是固定的,趕超前面或者被後面的趕超都是要回頭的,所以,假設木棒無限長,位於第一位的永遠在第一位,第二位的永遠在第二位。同時每兩個碰頭的螞蟻其實就是類似於交換了身份繼續前進。所以,先不管他們的碰頭,就讓每個沿著路線繼續走下去。由前面的可以知道,每個螞蟻的最終狀態的位置排列的順序就是一開始螞蟻的編號。
#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|1using 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;}