Uva 省赛傻逼题 B
要不要这么坑,自己的程序总是漏洞百出,不是这种情况没写好,就是那种情况没算进去,分析也不能很好的理论分析!
Problem B
Time limit: 1.000 seconds
Boxes in a Line
You have n boxes in a line on the table numbered 1~n from left to right. Your task is to simulate 4 kinds of commands:
- 1 X Y: move box X to the left to Y (ignore this if X is already the left of Y)
- 2 X Y: move box X to the right to Y (ignore this if X is already the right of Y)
- 3 X Y: swap box X and Y
- 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y.
For example, if n=6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing 2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1. Then after executing 4, then line becomes 1 3 5 4 6 2
Input
There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m(1<=n, m<=100,000). Each of the following m lines contain a command.
Output
For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to n from left to right.
Sample Input
6 4 1 1 4 2 3 5 3 1 6 4 6 3 1 1 4 2 3 5 3 1 6 100000 1 4
Output for the Sample Input
Case 1: 12 Case 2: 9 Case 3: 2500050000
The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define maxn 1000005 12 #define ll long long 13 #define INF 0x7fffffff 14 int p[2][maxn][2]; 15 int n,m; 16 void init(){ 17 memset(p,0,sizeof p); 18 for(int i=1;i<=n;i++){ 19 p[0][i][0]=i-1; 20 p[0][i][1]=i+1; 21 p[1][n-i+1][0]=n-i+2; 22 p[1][n-i+1][1]=n-i; 23 } 24 p[0][0][1]=1; 25 p[0][n+1][0]=n; 26 p[1][n+1][1]=n; 27 p[1][0][0]=1; 28 } 29 void fun1(int t,int a,int b){ 30 int al=p[t][a][0]; 31 int ar=p[t][a][1]; 32 if(ar==b)return; 33 p[t][al][1]=ar; 34 p[t][ar][0]=al; 35 int bl=p[t][b][0]; 36 p[t][bl][1]=a; 37 p[t][a][0]=bl; 38 p[t][a][1]=b; 39 p[t][b][0]=a; 40 t^=1; 41 al=p[t][a][0]; 42 ar=p[t][a][1]; 43 p[t][al][1]=ar; 44 p[t][ar][0]=al; 45 int br=p[t][b][1]; 46 p[t][br][0]=a; 47 p[t][a][1]=br; 48 p[t][a][0]=b; 49 p[t][b][1]=a; 50 } 51 void fun2(int t,int a,int b){ 52 int al=p[t][a][0]; 53 int ar=p[t][a][1]; 54 if(al==b)return; 55 p[t][al][1]=ar; 56 p[t][ar][0]=al; 57 int br=p[t][b][1]; 58 p[t][br][0]=a; 59 p[t][a][1]=br; 60 p[t][a][0]=b; 61 p[t][b][1]=a; 62 t^=1; 63 al=p[t][a][0]; 64 ar=p[t][a][1]; 65 p[t][al][1]=ar; 66 p[t][ar][0]=al; 67 int bl=p[t][b][0]; 68 p[t][bl][1]=a; 69 p[t][a][0]=bl; 70 p[t][a][1]=b; 71 p[t][b][0]=a; 72 } 73 void fun3(int t,int a,int b){ 74 int al=p[t][a][0]; 75 int ar=p[t][a][1]; 76 int bl=p[t][b][0]; 77 int br=p[t][b][1]; 78 if(ar!=b&&al!=b){ 79 p[t][al][1]=b; 80 p[t][b][0]=al; 81 p[t][ar][0]=b; 82 p[t][b][1]=ar; 83 p[t][bl][1]=a; 84 p[t][a][0]=bl; 85 p[t][br][0]=a; 86 p[t][a][1]=br; 87 t^=1; 88 al=p[t][a][0]; 89 ar=p[t][a][1]; 90 bl=p[t][b][0]; 91 br=p[t][b][1]; 92 p[t][al][1]=b; 93 p[t][b][0]=al; 94 p[t][ar][0]=b; 95 p[t][b][1]=ar; 96 p[t][bl][1]=a; 97 p[t][a][0]=bl; 98 p[t][br][0]=a; 99 p[t][a][1]=br; 100 return; 101 } 102 if(ar==b){ 103 p[t][al][1]=b; 104 p[t][b][0]=al; 105 p[t][a][0]=b; 106 p[t][b][1]=a; 107 p[t][br][0]=a; 108 p[t][a][1]=br; 109 t^=1; 110 al=p[t][a][0]; 111 ar=p[t][a][1]; 112 bl=p[t][b][0]; 113 br=p[t][b][1]; 114 p[t][bl][1]=a; 115 p[t][a][0]=bl; 116 p[t][a][1]=b; 117 p[t][b][0]=a; 118 p[t][ar][0]=b; 119 p[t][b][1]=ar; 120 return; 121 } 122 if(al==b){ 123 p[t][bl][1]=a; 124 p[t][a][0]=bl; 125 p[t][b][0]=a; 126 p[t][a][1]=b; 127 p[t][ar][0]=b; 128 p[t][b][1]=ar; 129 t^=1; 130 al=p[t][a][0]; 131 ar=p[t][a][1]; 132 bl=p[t][b][0]; 133 br=p[t][b][1]; 134 p[t][al][1]=b; 135 p[t][b][0]=al; 136 p[t][a][0]=b; 137 p[t][b][1]=a; 138 p[t][br][0]=a; 139 p[t][a][1]=br; 140 } 141 } 142 ll sum(int t){ 143 ll ans=0; 144 ll x=0; 145 if(t==0)x=0; 146 else x=n+1; 147 for(int i=0;i<n;i++){ 148 x=p[t][x][1]; 149 if(i%2==0)ans+=x; 150 } 151 return ans; 152 } 153 int main(){ 154 int cas=1; 155 while(~scanf("%d%d",&n,&m)){ 156 init(); 157 int t=0; 158 for(int i=0;i<m;i++){ 159 int op,a,b; 160 scanf("%d",&op); 161 if(op==4){t^=1;continue;} 162 scanf("%d%d",&a,&b); 163 if(op==1)fun1(t,a,b); 164 if(op==2)fun2(t,a,b); 165 if(op==3&&a!=b)fun3(t,a,b); 166 } 167 printf("Case %d: ",cas++); 168 printf("%lld\n",sum(t)); 169 } 170 return 0; 171 }
浙公网安备 33010602011771号