https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822
题意:在一根棍子上,有n个?,然后每个?都有一个初始的位置,以及头的朝向,然后每一秒移动一个单位的距离,两只蚂蚁互撞后互换反向,问t秒后,它们各自所在的位置。
思路:明确一个事情,就是说对于每一个?,它所在的位置的顺序是一定的,只不过后来的位置每个都改变了,因为是互换方向,而不是穿透而过。
所以我们需要是找出它的初始的位置顺序,然后每个记录一下,最后T秒后的位置,按这个排序,然后找到对应位置输出就可以了
注意0这个位置也还是在棍子上
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 struct Node{ 7 int x; 8 char r; 9 int loc; 10 }node[10005]; 11 12 bool cmp(const Node &a,const Node &b) 13 { 14 return a.x<b.x; 15 } 16 17 18 int main() 19 { 20 // freopen("ina.txt","w",stdout); 21 // freopen("in.txt","r",stdin); 22 int l,t,n,T; 23 int loc[10005]; 24 scanf("%d",&T); 25 for(int cnt = 1;cnt<=T;cnt++) 26 { 27 scanf("%d%d%d",&l,&t,&n); 28 // printf("%d %d %d\n",l,t,n); 29 // printf("password\n"); 30 for(int i = 1;i<=n;i++) 31 { 32 scanf("%d %c",&node[i].x,&node[i].r); 33 // printf("%d %c\n",node[i].x,node[i].r); 34 node[i].loc = i; 35 } 36 node[0].x = node[n+1].x = -5; 37 stable_sort(node+1,node+n+1,cmp); 38 for(int i = 1;i<=n;i++) 39 { 40 loc[node[i].loc] = i; 41 if(node[i].r=='R') 42 node[i].x+=t; 43 else 44 node[i].x-=t; 45 } 46 stable_sort(node+1,node+n+1,cmp); 47 printf("Case #%d:\n",cnt); 48 for(int i =1;i<=n;i++) 49 { 50 if(node[loc[i]].x<0||node[loc[i]].x>l) 51 printf("Fell off\n"); 52 else { 53 printf("%d ",node[loc[i]].x); 54 if(node[loc[i]-1].x==node[loc[i]].x||node[loc[i]+1].x==node[loc[i]].x) 55 printf("Turning\n"); 56 else 57 printf("%c\n",node[loc[i]].r); 58 } 59 } 60 printf("\n"); 61 } 62 return 0; 63 }