標籤:style blog color io os ar for 資料 div
此題取巧了,Usaco在這題上並沒有指明不可以用分析法,而且dfs肯定TLE,所以我們取巧。
先觀察範例資料,如果把還沒移動的那一步也算上,那麼空格的位置為
4 3 5 6 4 2 1 3 5 7 6 4 2 3 5 4 (n=3,範例)
5 4 6 7 5 3 2 4 6 8 9 7 5 3 1 2 4 6 8 7 5 3 4 6 5 (n=4)
我們憑藉極其敏銳的眼光發現這組序列為
435 642 1357 642 35 4 (n=3,範例)
5 46 753 2468 97531 2468 753 46 5 (n=4)
即長度為1,2,3,4,...,n,n+1,n,...,4,3,2,1這樣的2n+1組等差序列
我們討論第1~n+1組序列,這些序列滿足
*公差的絕對值為2
*奇數組為降序列,偶數組為升序列
*對於第i組(1<=i<=n+1),若為奇數組則首項為n+i,偶數組則首項為n-i+2
對於第n+2~2n+1組,可以由對稱性求出。
輸出時從第二組開始即可。
把規律總結成這樣後,代碼應該很好寫了吧。
/*ID: qq104801LANG: C++TASK: shuttle*/#include <iostream>#include <fstream>#include <cstring>#include <vector>#include <queue>#include <stack>#include <algorithm>using namespace std;void test(){ freopen("shuttle.in","r",stdin); freopen("shuttle.out","w",stdout); int n,p,s=0,k; cin>>n; p=n+1; for(int i=2;i<=2*n+1;i++) { k=(i<=n+1)?i:2*n+2-i; p+=(k&1)^(i>n+1)?1:-1; s++; cout<<p<<((s%20==0||i==2*n+1) ? ‘\n‘ : ‘ ‘); for(int j=2;j<=k;j++) p+=(k&1)?-2:2,s++, cout<<p<<((s%20)?‘ ‘:‘\n‘); } }int main () { test(); return 0;}
test data:
USACO TrainingGrader Results 14 users onlineBRA/1 CHN/4 IDN/1 IND/1 KOR/1 PER/1 USA/4 VNM/1USER: cn tom [qq104801]TASK: shuttleLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.008 secs, 3372 KB] Test 2: TEST OK [0.011 secs, 3372 KB] Test 3: TEST OK [0.011 secs, 3372 KB] Test 4: TEST OK [0.008 secs, 3372 KB] Test 5: TEST OK [0.005 secs, 3372 KB] Test 6: TEST OK [0.008 secs, 3372 KB] Test 7: TEST OK [0.008 secs, 3372 KB] Test 8: TEST OK [0.011 secs, 3372 KB] Test 9: TEST OK [0.008 secs, 3372 KB] Test 10: TEST OK [0.016 secs, 3372 KB]All tests OK.YOUR PROGRAM (‘shuttle‘) WORKED FIRST TIME! That‘s fantastic -- and a rare thing. Please accept these special automated congratulations.Here are the test data inputs:------- test 1 ----1------- test 2 ----3------- test 3 ----4------- test 4 ----5------- test 5 ----7------- test 6 ----8------- test 7 ----9------- test 8 ----10------- test 9 ----11------- test 10 ----12Keep up the good work!Thanks for your submission!
usaco-4.4-shutter-passed