UVA 10881 Piotr's Ants (analog)

Topic links: https://vjudge.net/problem/UVA-10881

 

In fact, the key to this question is only one sentence:

   When two ants turn around due to collisions, we can consider two points to wear off.

At this time our main task is to find out "who is who."

However, it is clear that the relative order of each ant is constant, so we have to record a $ order $ array.

Pretreatment its initial state and the order, the state after the same time was calculated walking.

Note that in some places in the program process is very clever.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 const int maxn=10005;
 8 struct node{
 9     int id;
10     int pos;
11     int dir;
12     bool operator < (const node& a) const{
13         return pos<a.pos;
14     }
15 } before[maxn],after[maxn];
16 const char dirname[][10]={"L","Turning","R"};
17 int order[maxn]; 
18 
19 int main(){
20     int kase;
21     scanf("%d",&kase);
22     for(int k=1;k<=kase;k++){
23         int L,T,n;
24         printf("Case #%d:\n",k);
25         scanf("%d%d%d",&L,&T,&n);
26         for(int i=0;i<n;i++){
27             int p,d;
28             char c;
29             scanf("%d %c",&p,&c);
30             d=(c=='L'?-1:1);
31             before[i]=(node){i,p,d};
32             after[i]=(node){0,p+d*T,d};
33         }
34         sort(before,before+n);
35         for(int i=0;i<n;i++) order[before[i].id]=i;
36         sort(after,after+n);
37         for(int i=0;i<n-1;i++)
38             if(after[i].pos==after[i+1].pos) after[i].dir=after[i+1].dir=0;
39         for(int i=0;i<n;i++){
40             int a=order[i];
41             if(after[a].pos<0||after[a].pos>L) printf("Fell off\n");
42             else printf("%d %s\n",after[a].pos,dirname[after[a].dir+1]);
43         }
44         printf("\n"); 
45     }
46     return 0;
47 } 
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/12241574.html