UVa 10881.Piotr's Ants【RuijiaLiu随书练习】【10月7】

Piotr's Ants

蚂蚁碰撞,相当于对穿而过。过一段时间以后蚂蚁的相对位置不变。代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
struct ants {
    int id, p, d;
    bool operator < (const ants& a) const {
        return a.p < p;
    }
};
bool cmp (ants a, ants b) {
    return a.id < b.id;
}
int main() {
    int N, kase = 1;
    scanf ("%d", &N);
    while (N--) {
        int l, t, n;
        char dir;
        ants before[maxn], after[maxn];
        scanf ("%d%d%d", &l, &t, &n);
        for (int i = 0; i < n; i++) {
            scanf ("%d %c", &before[i].p, &dir);
            before[i].id = i;
            if (dir == 'R') {
                after[i].p = before[i].p + t;
                after[i].d = 1;
            } else {
                after[i].p = before[i].p - t;
                after[i].d = -1;
            }
        }
        sort (before, before + n);
        sort (after, after + n);
        for (int i = 0; i < n; i++) {
            after[i].id = before[i].id;
            if (i < n - 1 && after[i].p == after[i + 1].p)
                after[i].d = after[i + 1].d = 0;
        }
        sort (after, after + n, cmp);
        printf ("Case #%d:\n", kase++);
        for (int i = 0; i < n; i++) {
            if (after[i].p < 0 || after[i].p > l) printf ("Fell off\n");
            else if (after[i].d == 1) printf ("%d R\n", after[i].p);
            else if (after[i].d == -1) printf ("%d L\n", after[i].p);
            else printf ("%d Turning\n", after[i].p);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(C++,ACM,uva,ants,RujiaLiu随书练习)