UVA - 10881 Piotr's Ants

题意:一根长L的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1,当两只蚂蚁相遇时,两者同时掉头(掉头时间忽略不计),给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂蚁的位置。计算T秒后每只蚂蚁的状态,注意要按输入时的顺序打印,

思路:之前有一题简单的是求最短的和最长的时间,其实每只蚂蚁的相对位置是不变的,我们需要记录的是每只蚂蚁T秒后的位置(按输入时候的顺序记录)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 10005;

struct Ant{
    int id;
    int p;
    int d;
    bool operator < (const Ant &a)const{
        return p < a.p;
    }
}before[MAXN],after[MAXN];

const char dirName[][10] = {"L","Turning","R"};

int order[MAXN];

int main(){
    int t;
    scanf("%d",&t);
    for (int cas = 1; cas <= t; cas++){
        int L,T,n;
        printf("Case #%d:\n",cas);
        scanf("%d%d%d",&L,&T,&n);
        for (int i = 0; i < n; i++){
            int p,d;
            char c;
            scanf("%d %c",&p,&c);
            d = (c=='L' ? -1 : 1);
            before[i] = (Ant){i,p,d};
            after[i] = (Ant){0,p+T*d,d};
        }
        sort(before,before+n);
        for (int i = 0; i < n; i++)
            order[before[i].id] = i;
        sort(after,after+n);
        for (int i = 0; i < n-1; i++)
            if (after[i].p == after[i+1].p)
                after[i].d = after[i+1].d = 0;
        for (int i = 0; i < n; i++){
            int a = order[i];
            if (after[a].p < 0 || after[a].p > L)
                printf("Fell off\n");
            else printf("%d %s\n",after[a].p,dirName[after[a].d+1]);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(UVA - 10881 Piotr's Ants)