首页 > 代码库 > UVA10881 Piotr's Ants

UVA10881 Piotr's Ants

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, NNtest 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 Sample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

 

 

题解:

这个题目我们可以发现每次相碰,他们的相对位置都不会发生改变,所以我们可以想如果两只蚂蚁相互碰撞,可以看成他们相互穿过去了,只是编号交换了一下,所以们对每只蚂蚁都这样移动跑一遍,从小到大排序,因为相对不位置发生改变,所以我们只要记一下一开始的相对位置然后直接选取就可以了。

 

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
const int MAXN=200010;
using namespace std;
struct people{
    int id,place,to;
}a[MAXN];
struct query{
    int id,ren,move;
}q[MAXN];
int rak[MAXN],wei[MAXN],ans[MAXN];
int n,h,flag=1;
 
bool cmp(people x,people y){return x.place<y.place;}
bool comp(query x,query y){return x.move<y.move;}
 
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].place);a[i].id=i;
    }
    int turn;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].to);
        if(i==0) turn=a[i].to;
        if(turn!=a[i].to) flag=0;
    }
    scanf("%d",&h);
    if(flag){
        while(h--){
            int k,t;scanf("%d%d",&k,&t);
            if(turn==0) printf("%d\n",a[k].place-t);
            else printf("%d\n",a[k].place+t);
        }
    }
    else{
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++) rak[a[i].id]=i;
        for(int i=1;i<=h;i++) {scanf("%d%d",&q[i].ren,&q[i].move);q[i].id=i;}
        sort(q+1,q+h+1,comp);
        int now=1;
        while(now<=h){
            int tim=q[now].move;
            for(int i=0;i<n;i++){
                if(a[i].to==0) wei[i]=a[i].place-tim;
                else wei[i]=a[i].place+tim;
            }
            sort(wei,wei+n);
            while(q[now].move==tim) ans[q[now].id]=wei[rak[q[now].ren]],now++;
        }
        for(int i=1;i<=h;i++) printf("%d\n",ans[i]);
    }
}

 

UVA10881 Piotr's Ants