uva10881 Piotr's Ants
刘汝佳《算法竞赛入门经典训练指南》1.1例5
1 ///2014.4.13 2 ///10881 3 ///刘汝佳《算法竞赛入门经典训练指南》1.1例5 4 5 #include <iostream> 6 #include <cstdio> 7 #include <string> 8 #include <cmath> 9 #include <algorithm> 10 #include <climits> 11 using namespace std; 12 13 struct Ant{ 14 int loc; ///location of the ant 15 int dir; ///direction , left:-1 , right:1 16 int id; ///the number of cin order 17 }; 18 int L,T,n; 19 Ant ant[10100]; 20 int loc[10100]; 21 bool cmp(Ant a,Ant b){ 22 return a.loc<b.loc; 23 } 24 bool cmp2(Ant a,Ant b){ 25 return a.id<b.id; 26 } 27 int main() 28 { 29 // freopen("in","r",stdin); 30 // freopen("out","w",stdout); 31 int t; 32 cin>>t; 33 int cas = 1; 34 while( t-- ){ 35 cin>>L>>T>>n; 36 for(int i=0 ; i<n ; i++){ 37 cin>>ant[i].loc; 38 char temp; 39 cin>>temp; 40 switch( temp ){ 41 case 'L': ant[i].dir = -1; break; 42 case 'R': ant[i].dir = 1; break; 43 } 44 ant[i].id = i; 45 } 46 sort(ant,ant+n,cmp); 47 for(int i=0 ; i<n ; i++){ 48 loc[i] = ant[i].id; 49 } 50 for(int i=0 ; i<n ; i++){ 51 ant[i].loc += ant[i].dir * T; 52 } 53 sort(ant,ant+n,cmp); 54 for(int i=0 ; i<n ; i++){ 55 if( (ant[i].loc==ant[i+1].loc) && i+1<n ){ 56 ant[i].dir=ant[i+1].dir=0; 57 i++; 58 } 59 } 60 for(int i=0 ; i<n ; i++){ 61 ant[i].id = loc[i]; 62 } 63 sort(ant,ant+n,cmp2); 64 cout<<"Case #"<<cas++<<':'<<endl; 65 for(int i=0 ; i<n ; i++){ 66 if( ant[i].loc<0 || ant[i].loc>L ) 67 cout<<"Fell off"<<endl; 68 else{ 69 cout<<ant[i].loc<<" "; 70 switch( ant[i].dir ){ 71 case 1: cout<<"R"<<endl; break; 72 case 0: cout<<"Turning"<<endl; break; 73 case -1: cout<<"L"<<endl;break; 74 } 75 } 76 } 77 cout<<endl; 78 } 79 return 0; 80 }