写这道题目的时候遇到了一个令人诧异的问题,就是平台上跑来的结果和我本机跑起来的结果不一样。
后来Debug了之后才发现是我数组开小了,只开到100 的数组竟然都去访问他170位的地址肯定要跪成翔啊...
好吧,解释一下题意。
有N盏台灯,C次操作
每次操作可以按一次按钮,一共一个四个按钮
可以得出的规律是每6个台灯是一次循环。
易得证。(其实不难发现Button 1 不用考虑,Button 2 和 Button 3 对立, 那么Button 4 的作用就是让1, 4, 7, 11改变状态
那么可以发现 7 和 1 其实是一模一样的,他们都受Button 1, 3, 4操控,依次类推即可)
还不会用BitSet来写这道题目...得学习一下了,我只开了个数组sta_ans[w][i][j][k][l][m][n] 来代表第w次操作后,后面六个代表的是二进制
这样的话每次访问都是O(1)的效率会非常快
结果:
USER: Jeremy Wu [wushuai2] TASK: lamps LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.011 secs, 5996 KB] Test 2: TEST OK [0.005 secs, 5996 KB] Test 3: TEST OK [0.005 secs, 5996 KB] Test 4: TEST OK [0.005 secs, 5996 KB] Test 5: TEST OK [0.005 secs, 5996 KB] Test 6: TEST OK [0.005 secs, 5996 KB] Test 7: TEST OK [0.003 secs, 5996 KB] Test 8: TEST OK [0.005 secs, 5996 KB] All tests OK.
/* ID: wushuai2 PROG: lamps LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #include <list> #include <queue> #include <vector> #include <algorithm> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (((a) < (b)) ? (a) : (b)) #define Abs(x) (((x) > 0) ? (x) : (-(x))) #define MOD 1000000007 #define pi acos(-1.0) #define RV(num) ((num) > 0 ? 0 : 1) using namespace std; typedef long long ll ; typedef unsigned long long ull ; typedef unsigned int uint ; typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;} template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e-7 ; const int M = 660000 ; const ll P = 10000000097ll ; const int INF = 0x3f3f3f3f ; const int MAX_N = 20 ; const int MAXSIZE = 101000000; int N, C; int ans[10], sum; int sta_ans[10011][2][2][2][2][2][2]; bool check(int i, int j, int k, int l, int m, int n){ int aaa[] = {0, i, j, k, l, m, n}; for(int ii = 1; ii < 7; ++ii){ if(ans[ii] == -1 || aaa[ii] == ans[ii]){ continue; } else return false; } return true; } int main() { ofstream fout ("lamps.out"); ifstream fin ("lamps.in"); int i, j, k, l, m, n, t, s, c, w, q, num; memset(ans, -1, sizeof(ans)); fin >> N; fin >> C; while(fin >> num){ if(-1 == num) break; if(num % 6 == 0) ans[6] = 1; else ans[num % 6] = 1; } while(fin >> num){ if(-1 == num) break; if(num % 6 == 0) ans[6] = 0; else ans[num % 6] = 0; } sta_ans[0][1][1][1][1][1][1] = 1; for(w = 1; w <= C; ++w){ for(i = 0; i < 2; ++i){ for(j = 0; j < 2; ++j){ for(k = 0; k < 2; ++k){ for(l = 0; l < 2; ++l){ for(m = 0; m < 2; ++m){ for(n = 0; n < 2; ++n){ if(sta_ans[w - 1][i][j][k][l][m][n] == 1){ sta_ans[w][RV(i)][RV(j)][RV(k)][RV(l)][RV(m)][RV(n)] = 1; sta_ans[w][i][RV(j)][k][RV(l)][m][RV(n)] = 1; sta_ans[w][RV(i)][j][RV(k)][l][RV(m)][n] = 1; sta_ans[w][RV(i)][j][k][RV(l)][m][n] = 1; } } } } } } } } for(i = 0; i < 2; ++i){ for(j = 0; j < 2; ++j){ for(k = 0; k < 2; ++k){ for(l = 0; l < 2; ++l){ for(m = 0; m < 2; ++m){ for(n = 0; n < 2; ++n){ if(check(i, j, k, l, m, n)){ if(sta_ans[C][i][j][k][l][m][n] == 1){ ++sum; for(int ii = 1; ii <= N; ++ii){ if(ii % 6 == 0){ fout << n; } else if(ii % 6 == 1){ fout << i; } else if(ii % 6 == 2){ fout << j; } else if(ii % 6 == 3){ fout << k; } else if(ii % 6 == 4){ fout << l; } else if(ii % 6 == 5){ fout << m; } } fout << endl; } } } } } } } } if(!sum){ fout << "IMPOSSIBLE" << endl; } fin.close(); fout.close(); return 0; }
USACO Party Lamps 【Binary code solvution】【规律】
原文:http://www.cnblogs.com/wushuaiyi/p/4296755.html