USACO 4.4 Shuttle Puzzle(数学)
2015-03-25 22:21:59
思路:一道找规律的题... 一开始没啥思路,看了别人博客才知道...
N=3:3 5 6 4 2 1 3 5 7 6 4 2 3 5 4
差: 2 1 -2 -2 -1 2 2 2 -1 -2 -2 1 2 -1
N=4:4 6 7 5 3 2 4 6 8 9 7 5 3 1 2 4 6 8 7 5 3 4 6 5
差: 2 1 -2 -2 -1 2 2 2 1 -2 -2 -2 -2 1 2 2 2 -1 -2 -2 1 2 -1
N=5:5 7 8 6 4 3 5 7 9 10 8 6 4 2 1 3 5 7 9 11 10 8 6 4 2 3 5 7 9 8 6 4 5 7 6
差: 2 1 -2 -2 -1 2 2 2 1 -2 -2 -2 -2 1 2 2 2 2 2 -1 -2 -2 -2 -2 1 2 2 2 -1 -2 -2 1 2 -1
规律竟然在差里面... orz,然后我的做法就是从两边向中间逼近... 有点小麻烦。
1 /* 2 ID:naturec1 3 PROG: shuttle 4 LANG: C++ 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cstdlib> 9 #include <cmath> 10 #include <vector> 11 #include <map> 12 #include <set> 13 #include <stack> 14 #include <queue> 15 #include <string> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 20 #define MEM(a,b) memset(a,b,sizeof(a)) 21 #define REP(i,n) for(int i=1;i<=(n);++i) 22 #define REV(i,n) for(int i=(n);i>=1;--i) 23 #define FOR(i,a,b) for(int i=(a);i<=(b);++i) 24 #define RFOR(i,a,b) for(int i=(a);i>=(b);--i) 25 #define getmid(l,r) ((l) + ((r) - (l)) / 2) 26 #define MP(a,b) make_pair(a,b) 27 #define PB(a) push_back(a) 28 29 typedef long long ll; 30 typedef pair<int,int> pii; 31 const int INF = (1 << 30) - 1; 32 33 int N; 34 vector<int> ans1,ans2; 35 36 int main(){ 37 freopen("shuttle.in","r",stdin); 38 freopen("shuttle.out","w",stdout); 39 scanf("%d",&N); 40 if(N == 1){ 41 printf("1 3 2\n"); 42 return 0; 43 } 44 ans1.PB(2); ans1.PB(1); 45 int f = -1; 46 for(int i = 1; i <= N - 2; ++i){ 47 for(int j = 1; j <= i + 1; ++j) ans1.PB(2 * f); 48 ans1.PB(f); 49 f *= -1; 50 } 51 for(int i = 1; i <= N; ++i) ans1.PB(2 * f); 52 f = -1; 53 ans2.PB(-1); ans2.PB(2); ans2.PB(1); 54 for(int i = 1; i <= N - 2; ++i){ 55 for(int j = 1; j <= i + 1; ++j) ans2.PB(2 * f); 56 ans2.PB(f); 57 f *= -1; 58 } 59 int st = N; 60 int tot = 1; 61 printf("%d",st); 62 for(int i = 0; i < ans1.size(); ++i){ 63 st += ans1[i]; 64 if(tot % 20) printf(" "); 65 printf("%d",st); 66 if((++tot) % 20 == 0) puts(""); 67 } 68 for(int i = ans2.size() - 1; i >= 0; --i){ 69 st += ans2[i]; 70 if(tot % 20) printf(" "); 71 printf("%d",st); 72 if((++tot) % 20 == 0) puts(""); 73 } 74 if(tot % 20) puts(""); 75 return 0; 76 }