【UVa 10881】Piotr's Ants

Piotr's Ants

Porsition:Uva 10881 白书P9

中文改编题:【T^T】【FJUT】第二届新生赛真S题地震了

"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 horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and 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. N test 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 (measured in 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 the n ants in the same format and order as in the input. If two or more ants are at the same location, print ‘Turning’ instead of ‘L’ or ‘R’ for their 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

Solution

脑洞大开,两只蚂蚁相撞返回相当于穿过?但保证每只蚂蚁初始的顺序.所以每只蚂蚁直接向左向右走,实际上它会穿过很多只蚂蚁,每穿过一次就变一次身,但他们的先后顺序是保证的,就是不会真正穿过去,只是用对面那只蚂蚁代替自己,所以只要记录蚂蚁排列顺序对应在原序列第几个即可。

福利数据

 戳这~

Code

// <ants.cpp> - Mon Oct 10 16:18:55 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=10010;
inline int gi() {
	register int w=0,q=0;register char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')q=1,ch=getchar();
	while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
	return q?-w:w;
}
struct node{
    int p,id;char c;
    bool operator<(node b)const{return p<b.p;}
}a[MAXN];int d[MAXN];
int main()
{
	freopen("ants.in","r",stdin);
	freopen("ants.out","w",stdout);
    int T=gi();
    for(int o=1;o<=T;o++){
        printf("Case #%d:
",o);
        int l=gi(),t=gi(),n=gi();
        for(int i=1;i<=n;i++)scanf("%d %c",&a[i].p,&a[i].c),a[i].id=i;
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
            d[a[i].id]=i,a[i].p-=(a[i].c=='L'?1:-1)*t;
        sort(a+1,a+1+n);
        for(int i=1,x;x=d[i],i<=n;i++)
            if((a[x].p==a[x-1].p&&x-1)||(x+1<=l&&a[x].p==a[x+1].p))printf("%d Turning
",a[x].p);
            else if(a[x].p>=0&&a[x].p<=l)printf("%d %c
",a[x].p,a[x].c);
            else printf("Fell off
");printf("
");
    }
	return 0;
}

  

原文地址:https://www.cnblogs.com/YJinpeng/p/5950689.html