UVa 10881 - Piotr's Ants

Problem D
Piotr's Ants
Time Limit: 2 seconds

"One thing is for certain: there is no stopping them;
the ants will soon be here. And I, for one, welcome our
new insect overlords."
Kent Brockman

Piotr likes playing with ants. He has n of them on a horizontalpole L cm long. Each ant is facing either left or right and walksat a constant speed of 1 cm/s. When two ants bump into each other, theyboth turn around (instantaneously) and start walking in opposite directions.Piotr knows where each of the ants starts and which direction it is facingand wants to calculate where the ants will end up T seconds from now.

Input
The first line of input gives the number of cases, N. Ntest cases follow. Each one starts with a line containing 3 integers:L , T and n (0 <= n <= 10000) .The next n lines give the locations of the n ants (measuredin cm from the left end of the pole) and the direction they are facing(L or R).

Output
For each test case, output one line containing "Case #x:"followed by n lines describing the locations and directions of then ants in the same format and order as in the input. If two or moreants are at the same location, print "Turning" instead of "L" or "R" fortheir direction. If an ant falls off the pole before T seconds,print "Fell off" for that ant. Print an empty line after each test case.

Sample Input

2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Sample Output

Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R
两个蚂蚁相撞相当于交换了编号,其他信息均不变

/*************************************************************************
	> File Name: uva10881.cpp
	> Author: wjzdmr
	> Mail: wjzdmr@gmail.com
	> Created Time: 2014年09月22日 星期一 18时27分43秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>

using namespace std;
//ios_base::sync_with_stdio(0);
#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define sz(x) (int)((x).size())
#define sl(a) strlen(a)
#define rep(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define Rep(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define dbg(a) cout << a << endl;
#define fi first
#define se second
typedef long long int64;
int gcd(const int64 &a, const int64 &b) { return b == 0 ? a : gcd(b, a % b); }
int64 int64pow(int64 a, int64 b){ if (b == 0) return 1; int64 t = int64pow(a, b / 2); if (b % 2) return t * t * a; return t * t; }
const int inf = 0x7fffffff;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int MAX_N = 10005;

struct node {
    int pos;
    int to;
    int id;
};

node arr[MAX_N];
int T, cas = 0;
int l, Time, n;
char C;
int m[100000];

bool cmp (const node &a, const node &b) 
{
    return a.pos < b.pos;
}
void work()
{
    mem(m, 0);

    rep(i, 0, n) {
        scanf("%d %c", &arr[i].pos, &C);
        arr[i].to = C == 'L' ? -1 : 1;
        arr[i].id = i;
    }

    sort(arr, arr + n, cmp);

    rep(i, 0, n) {
        m[arr[i].id] = i;
        arr[i].pos += Time * arr[i].to;
    }

    sort(arr, arr + n, cmp);
    
    cout << "Case #" << ++cas << ":" << endl;

    rep(i, 0, n) {
        int t = m[i];
        
        if (arr[t].pos < 0 || arr[t].pos > l) {
            cout << "Fell off" << endl;
        }
        else if (arr[t].pos == arr[t - 1].pos || arr[t].pos == arr[t + 1].pos) {
            cout << arr[t].pos << " Turning" << endl;
        }
        else {
            if (arr[t].to == -1) {
                cout << arr[t].pos << " L" << endl;
            }
            else {
                cout << arr[t].pos << " R" << endl;
            }
        }
    }
    cout << endl;
}
int main()
{
    cin >> T;
    while (cin >> l >> Time >> n) {
        work();
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值