Uva10881 Piotr's Ants

蚂蚁相撞会各自回头。←可以等效成对穿而过,这样移动距离就很好算了。

末状态蚂蚁的顺序和初状态其实是相同的。

那么剩下的就是记录每只蚂蚁的标号,模拟即可。

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=10010;
10 struct ants{
11     int id,p,dir;
12     int pos;
13 }a[mxn];
14 int cmp(const ants x,const ants y){
15     return x.p<y.p;
16 }
17 int cmp2(const ants x,const ants y){
18     return x.pos<y.pos;
19 }
20 int l,t,n;
21 int mp[mxn];
22 int main(){
23     int T,i,j;
24     scanf("%d",&T);
25     int cas=0;
26     while(T--){
27         printf("Case #%d:
",++cas);
28         scanf("%d%d%d",&l,&t,&n);
29         char dr;
30         for(i=1;i<=n;i++){
31             scanf("%d %c",&a[i].p,&dr);
32             if(dr=='L') a[i].dir=-1;
33             else a[i].dir=1;
34         }
35         for(i=1;i<=n;i++){
36             a[i].id=i;
37             a[i].pos=a[i].p+t*a[i].dir;
38         }
39         sort(a+1,a+n+1,cmp);
40         for(i=1;i<=n;i++){
41             mp[a[i].id]=i;
42         }
43         sort(a+1,a+n+1,cmp2);
44         for(i=1;i<n;i++){
45             if(a[i].pos==a[i+1].pos){
46                 a[i].dir=a[i+1].dir=0;
47             }
48         }
49         for(i=1;i<=n;i++){
50             int thi=mp[i];
51             if(a[thi].pos<0 || a[thi].pos>l){
52                 printf("Fell off
");continue;
53             }
54             printf("%d ",a[thi].pos);
55             if(a[thi].dir==-1)printf("L
");
56             else if(a[thi].dir==1)printf("R
");
57             else printf("Turning
");
58         }
59         printf("
");
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/SilverNebula/p/6089740.html