PROB The Castle [ANALYSIS] ---- 挺好的一道题,不过做起来挺费时间!
我构图构成了普通的矩阵图,不是邻接阵或者邻接表,挺繁琐,不过还好通过了。
// solution 构图,搜索 /* solution 1 构图 共 2N + 2 行 2M + 2 列 墙用‘*’填充 '*' -3 不可走 'number' 房间号 ' ' -1 可走 '-' -2 has[number] 房间大小 2 DFS 3 枚举 墙 先最 西,再南(遍历方式改变) */ int n,m,chatmp,rooms,bigs,makbigs,remwallx,remwally,stax,stay,stan,stam; int mat[MAXN][MAXN]; char directions; int has[MAXN * MAXN] ={0}; void swit(int i,int j) { //DB(i<<" "<<j<<" "<<chatmp) switch(chatmp) { case 0 : mat[RR(i)-1][RR(j)] = -2; mat[RR(i)+1][RR(j)] = -2; mat[RR(i)][RR(j)+1] = -2; mat[RR(i)][RR(j)-1] = -2; break; case 1 : mat[RR(i)-1][RR(j)] = -2; mat[RR(i)+1][RR(j)] = -2; mat[RR(i)][RR(j)+1] = -2; //mat[RR(i)][RR(j)-1] = -2; break; case 2 : //mat[RR(i)-1][RR(j)] = -2; mat[RR(i)+1][RR(j)] = -2; mat[RR(i)][RR(j)+1] = -2; mat[RR(i)][RR(j)-1] = -2; break; case 4 : mat[RR(i)-1][RR(j)] = -2; mat[RR(i)+1][RR(j)] = -2; //mat[RR(i)][RR(j)+1] = -2; mat[RR(i)][RR(j)-1] = -2; break; case 5 : mat[RR(i)-1][RR(j)] = -2; //N mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E //mat[RR(i)][RR(j)-1] = -2; //W break; case 6 : //mat[RR(i)-1][RR(j)] = -2; //N mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E mat[RR(i)][RR(j)-1] = -2; //W break; case 7 : //mat[RR(i)-1][RR(j)] = -2; //N mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E //mat[RR(i)][RR(j)-1] = -2; //W break; case 8 : mat[RR(i)-1][RR(j)] = -2; //mat[RR(i)+1][RR(j)] = -2; mat[RR(i)][RR(j)+1] = -2; mat[RR(i)][RR(j)-1] = -2; break; case 9 : mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S mat[RR(i)][RR(j)+1] = -2; //E //mat[RR(i)][RR(j)-1] = -2; //W break; case 10: //mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S mat[RR(i)][RR(j)+1] = -2; //E mat[RR(i)][RR(j)-1] = -2; //W break; case 11: //mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S mat[RR(i)][RR(j)+1] = -2; //E //mat[RR(i)][RR(j)-1] = -2; //W break; case 12: mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E mat[RR(i)][RR(j)-1] = -2; //W break; case 13: mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E //mat[RR(i)][RR(j)-1] = -2; //W break; case 14: //mat[RR(i)-1][RR(j)] = -2; //N //mat[RR(i)+1][RR(j)] = -2; //S //mat[RR(i)][RR(j)+1] = -2; //E mat[RR(i)][RR(j)-1] = -2; //W break; case 15: break; } //PP(n*2 + 3,m*2+3,mat); } bool all() { F(i,n) F(j,m) { //DB(RR(i)<<" "<<RR(j)) if(mat[RR(i)][RR(j)] == -1) { stax = RR(i); stay = RR(j); return 0; } } return 1; } int step=0; void dfs(int x,int y) { step++; mat[x][y] = rooms; int tempx,tempy; tempx = x + 1;tempy = y; if(mat[tempx][tempy] != -3 && mat[tempx + 1][tempy] == -1) { dfs(tempx + 1,tempy); } tempx = x;tempy = y + 1; if(mat[tempx][tempy] != -3 && mat[tempx][tempy + 1] == -1) { dfs(tempx,tempy + 1); } tempx = x - 1;tempy = y; if(mat[tempx][tempy] != -3 && mat[tempx - 1][tempy] == -1) { dfs(tempx - 1,tempy); } tempx = x;tempy = y - 1; if(mat[tempx][tempy] != -3 && mat[tempx][tempy - 1] == -1) { dfs(tempx,tempy - 1); } } int main() { //FOPEN FOPENTI FOPENTO SCFD(m,n); stan = RR(n);stam = RR(m); SET(mat,-1); F(i,n) F(j,m) { mat[0][j] = mat[i][0] = -3; mat[0][j+m] = mat[i+n][0] = -3; mat[LL(i)][j] = mat[i][LL(j)] = -3; mat[LL(i)][j+m] = mat[i+n][LL(j)] = -3; } F(i,stan) mat[i][LL(m)] = -3; F(j,stam) mat[LL(n)][j] = -3; F(i,n) F(j,m) { SCF(chatmp); /* if(chatmp == 15) dfs() else */ swit(i,j); } //PP(n*2 + 3,m*2+3,mat); bigs = INT_MIN; directions = 'o'; //step 2 DFS while(!all()) { step =0; dfs(stax,stay); has[rooms]=step; rooms ++; bigs = max(bigs,step); //PP(n*2 + 3,m*2+3,mat); } //PP(n*2 + 3,m*2+3,mat); for(int j = 0;j<m;j++) { // j 列 //房间的 S 墙 int tey = RR(j); for(int i = n-1;i>0;i--) { int tex = LL(i); // 是墙 if(mat[tex][tey] == -3) { // 两边是不同的连通量 if(mat[tex + 1][tey] != mat[tex - 1][tey]) { // 取最大 int broom = has[mat[tex+1][tey]] + has[mat[tex-1][tey]]; if(makbigs < broom) { makbigs = broom; remwallx = i + 1; remwally = j + 1; directions = 'N'; } } } } //房间的 E 墙 if(j == m-1) break; tey = LL(j+1); for(int i = n-1;i>=0;i--) { int tex = RR(i); // 是墙 if(mat[tex][tey] == -3) { // 两边是不同的连通量 if(mat[tex][tey + 1] != mat[tex][tey - 1]) { // 取最大 int broom = has[mat[tex][tey+1]] + has[mat[tex][tey-1]]; if(makbigs < broom) { makbigs = broom; remwallx = i + 1; remwally = j + 1; directions = 'E'; } } } } } printf("%d\n%d\n%d\n%d %d %c\n",rooms,bigs,makbigs,remwallx,remwally,directions); }
代码里面,二倍关系转变据繁琐,导致了写这个代码用了好长时间。
USER: Rain M [m3324631] TASK: castle LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3144 KB] Test 2: TEST OK [0.000 secs, 3144 KB] Test 3: TEST OK [0.000 secs, 3144 KB] Test 4: TEST OK [0.000 secs, 3144 KB] Test 5: TEST OK [0.000 secs, 3144 KB] Test 6: TEST OK [0.000 secs, 3144 KB] Test 7: TEST OK [0.022 secs, 3144 KB] Test 8: TEST OK [0.011 secs, 3144 KB] All tests OK. Your program ('castle') produced all correct answers! This is your submission #3 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 7 4 11 6 11 6 3 10 6 7 9 6 13 5 15 5 1 10 12 7 13 7 5 13 11 10 8 10 12 13 ------- test 2 ---- 2 1 15 15 ------- test 3 ---- 5 5 3 2 6 3 6 1 8 4 1 4 13 7 13 9 4 3 0 2 6 5 9 8 8 12 13 ------- test 4 ---- 15 14 11 14 3 6 3 6 3 6 3 6 3 6 11 2 6 3 10 12 9 12 9 12 9 12 9 12 9 6 1 4 13 7 11 10 10 10 10 10 10 14 11 6 5 1 4 11 4 3 10 10 10 10 10 10 10 6 5 13 1 4 7 13 5 7 11 10 2 10 14 7 5 13 7 9 12 1 14 5 1 10 14 5 11 10 4 9 6 1 10 6 13 7 5 5 3 10 8 10 6 5 3 12 9 10 12 11 4 5 13 5 3 10 6 5 13 5 3 10 10 6 7 13 1 10 4 1 10 4 1 10 4 5 3 10 12 1 14 5 7 5 9 10 12 5 7 5 5 9 10 6 13 7 13 5 9 10 2 10 12 13 13 5 3 10 12 7 5 15 9 10 14 5 11 14 11 14 5 9 10 6 5 13 7 7 11 10 8 10 14 7 3 12 3 10 12 13 15 13 13 11 10 14 11 10 12 9 10 8 10 14 ------- test 5 ---- 10 10 11 10 10 14 7 3 10 2 2 6 3 2 2 6 5 1 2 0 0 4 1 0 0 4 5 1 0 0 0 4 1 0 0 4 13 9 8 8 8 4 1 0 0 4 7 11 10 10 14 13 1 0 0 4 5 7 3 2 2 6 1 0 0 4 5 5 1 0 0 4 1 0 0 4 13 5 1 0 0 4 1 0 0 4 7 5 1 0 0 4 9 8 8 12 13 13 9 8 8 12 ------- test 6 ---- 32 32 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 6 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 6 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 12 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 12 ------- test 7 ---- 50 50 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 ------- test 8 ---- 50 50 7 15 7 3 14 11 10 10 14 3 2 2 6 15 7 7 7 7 11 2 6 3 10 2 6 15 3 14 3 10 10 10 6 11 10 2 14 7 7 11 14 3 14 7 7 15 11 14 15 7 1 14 9 0 10 14 15 7 7 13 9 8 8 2 0 12 9 0 2 4 13 1 14 5 9 6 9 14 13 7 15 7 13 3 10 0 2 4 13 15 11 4 15 13 13 11 10 10 6 5 13 15 11 8 14 11 6 5 13 3 6 15 11 12 1 14 15 1 12 9 6 1 6 1 10 4 11 6 3 8 2 4 7 13 3 12 13 13 3 6 15 9 2 6 3 14 7 7 1 12 7 3 14 7 3 14 5 1 2 0 4 15 11 14 13 11 2 0 14 11 12 1 0 8 6 1 2 12 5 15 5 1 8 14 13 11 2 2 4 1 6 11 12 13 1 14 9 12 5 7 9 0 14 1 8 2 8 0 4 1 4 3 2 14 7 7 5 13 11 14 3 12 9 10 4 9 0 14 1 2 0 0 10 6 11 10 4 1 4 9 0 14 15 15 9 10 10 10 0 12 7 9 6 13 3 12 15 9 4 5 13 1 4 15 1 8 4 3 2 2 0 6 7 15 5 15 9 2 12 1 12 5 11 4 7 7 1 8 8 6 5 11 10 6 15 15 11 14 5 7 13 7 5 15 13 7 11 6 5 13 15 13 1 2 12 15 5 9 8 12 5 1 0 2 8 10 10 4 15 5 15 13 11 4 5 1 12 15 3 12 9 2 6 9 14 7 3 6 5 13 11 8 0 6 15 9 2 8 12 11 6 3 12 9 2 2 4 11 6 15 9 8 4 1 14 7 15 5 15 1 6 11 2 0 8 0 14 15 5 15 7 13 5 15 15 9 8 0 12 7 7 7 13 13 3 6 13 11 2 2 12 9 14 11 8 0 4 11 8 14 3 14 1 4 3 12 3 4 3 4 9 10 0 12 7 5 7 11 12 15 1 10 8 10 14 7 11 8 2 4 9 8 2 2 8 8 2 14 9 4 15 7 3 6 11 8 8 6 15 15 5 7 9 4 13 7 5 5 9 0 10 10 8 10 0 8 12 7 11 10 12 15 3 10 2 0 10 6 5 13 15 7 5 13 15 11 12 7 15 9 6 5 9 12 7 7 11 8 6 11 0 0 6 13 11 12 9 12 11 8 10 14 11 10 8 6 3 12 7 11 2 14 5 11 4 13 3 8 12 15 15 5 1 6 3 6 15 5 3 2 0 4 11 14 9 0 14 11 0 6 5 13 5 11 6 11 10 14 11 10 14 15 15 3 6 13 13 11 4 7 1 6 1 2 0 2 12 15 11 14 15 1 12 5 5 13 7 1 0 4 5 5 7 11 6 5 11 10 8 12 5 3 12 7 5 15 15 15 15 11 14 15 15 13 5 7 11 6 5 13 13 1 0 0 8 8 2 2 14 7 11 12 7 1 8 6 1 12 9 4 1 12 5 11 8 4 3 2 10 14 9 0 6 13 5 15 15 15 3 2 6 3 2 2 8 4 7 9 0 6 15 5 1 4 11 6 9 0 2 12 7 11 8 0 10 8 4 7 3 0 4 15 9 10 10 0 0 4 3 10 6 13 13 11 8 10 14 11 12 5 9 4 1 8 6 13 9 6 13 1 2 12 9 12 3 4 3 0 0 6 1 10 14 9 10 6 1 12 9 12 5 15 7 3 10 12 5 13 5 15 5 11 2 14 7 3 10 14 7 5 7 1 12 15 13 3 14 9 10 4 1 14 7 3 12 9 12 1 12 5 5 11 6 11 14 1 4 7 7 15 1 6 5 9 2 14 9 14 9 10 0 2 8 14 9 8 2 14 13 9 4 5 11 2 10 8 6 3 14 9 4 3 12 9 6 3 6 13 15 13 13 15 5 11 2 12 1 12 5 15 9 8 8 10 8 14 11 14 7 11 0 12 11 10 6 11 12 7 7 7 13 9 10 4 15 7 5 13 3 14 13 1 14 11 8 0 8 6 15 7 3 2 8 14 5 3 0 6 13 15 11 2 2 6 7 3 14 7 13 3 4 11 14 11 0 10 6 13 1 4 7 15 11 4 11 8 8 6 1 6 15 9 10 6 3 12 15 1 6 5 1 8 10 14 9 4 1 4 3 2 6 1 8 8 4 5 15 5 15 9 0 2 6 11 12 3 4 7 1 4 9 10 2 0 6 11 14 1 12 9 6 3 2 8 8 2 2 0 4 5 9 6 7 3 6 1 8 8 12 9 8 4 3 10 8 0 14 13 3 2 12 5 9 6 15 5 9 0 0 0 10 2 12 5 9 6 3 0 6 3 8 8 0 6 15 5 13 1 4 13 7 9 8 4 13 9 2 2 14 3 6 1 8 2 14 13 15 15 1 8 10 0 10 12 3 12 3 0 0 12 15 9 6 1 10 0 12 13 1 4 3 2 8 0 10 0 10 0 8 14 13 11 10 4 11 6 13 1 6 5 5 1 6 1 6 7 11 14 5 15 7 5 7 11 0 14 9 4 13 11 2 14 9 12 3 0 14 3 4 9 4 1 2 4 11 8 14 5 15 15 11 2 6 1 2 12 3 8 4 5 1 0 8 4 13 1 2 2 0 6 1 8 12 3 4 11 6 9 6 3 8 10 2 14 1 4 15 9 4 3 4 5 5 1 10 6 7 13 15 15 3 12 9 4 9 2 4 7 5 5 13 5 3 4 11 8 8 8 12 1 4 11 10 0 4 3 8 2 8 12 7 11 0 10 8 4 11 10 8 12 13 13 1 12 15 1 4 11 2 14 5 3 2 4 15 5 13 13 1 8 6 13 1 0 2 14 3 2 14 5 9 6 7 5 5 5 11 0 6 7 13 3 12 11 10 4 7 11 6 11 10 10 4 15 11 0 4 15 1 14 1 4 9 12 3 4 7 3 0 10 8 10 12 1 0 6 13 5 15 9 6 13 9 12 1 0 10 4 9 0 14 13 7 15 11 12 1 14 1 6 3 6 9 14 11 4 13 7 13 7 13 5 15 11 0 8 12 1 0 2 2 6 11 4 9 0 6 9 10 14 13 3 6 7 5 1 6 5 11 4 11 2 8 14 3 6 13 15 1 12 1 4 11 14 7 13 7 9 10 4 11 12 7 11 12 15 3 12 5 13 1 12 7 9 14 1 0 10 10 2 2 0 8 8 12 5 5 13 7 5 15 9 6 11 4 1 14 3 4 3 8 8 6 7 1 14 13 11 2 12 3 10 12 3 2 2 0 10 0 2 12 3 0 10 6 9 8 10 10 0 8 12 7 11 14 1 12 3 12 5 3 2 8 14 1 8 2 8 12 1 14 11 8 12 9 6 15 11 8 6 9 14 15 13 5 13 13 15 5 13 3 4 5 15 5 11 10 14 7 1 6 15 9 2 2 8 6 13 11 4 13 9 10 14 9 6 9 10 10 4 3 10 10 10 10 8 2 14 3 12 15 3 14 11 0 6 15 15 13 15 9 8 0 10 4 7 11 2 8 4 13 3 10 0 12 11 0 10 2 0 2 6 3 14 11 4 3 2 14 13 13 11 14 3 6 3 8 10 4 7 7 9 6 11 12 13 3 2 10 6 15 7 13 11 8 4 7 13 3 4 15 5 15 5 7 3 8 6 1 4 5 9 0 10 10 4 9 8 10 14 3 6 7 13 1 8 10 6 1 4 9 14 13 3 6 7 1 4 11 8 14 1 6 11 6 9 4 3 4 5 15 9 14 1 8 4 15 9 0 0 12 7 13 7 3 12 7 15 3 6 5 13 9 14 13 11 14 9 4 5 15 15 15 13 13 9 4 13 15 11 14 9 0 14 9 6 5 5 9 12 11 2 6 9 6 5 15 11 0 8 2 4 11 12 1 10 12 15 13 1 8 6 3 2 2 2 10 10 12 5 3 6 15 3 10 10 12 7 3 10 2 6 1 14 15 13 5 13 15 11 2 8 8 6 13 5 15 3 8 6 13 13 11 14 9 14 7 15 11 4 7 9 4 13 9 12 3 14 15 5 5 9 6 9 14 3 14 1 0 6 9 4 9 6 15 15 13 7 15 11 12 11 14 5 3 12 3 8 2 8 10 10 10 10 14 3 8 6 15 9 0 10 12 11 6 7 9 2 10 4 9 2 12 15 11 8 14 1 0 4 7 13 3 0 6 11 6 9 2 10 14 15 11 4 9 2 8 14 9 6 11 6 11 2 2 0 6 13 15 15 1 2 6 3 4 9 14 13 11 4 3 4 11 14 3 10 2 4 9 4 5 15 9 12 1 6 13 7 9 2 14 7 15 9 10 12 11 14 15 9 14 9 14 9 12 1 4 15 3 10 4 5 5 1 4 3 2 10 14 5 9 4 11 10 4 11 12 13 15 5 1 2 10 2 12 1 10 4 11 4 15 13 11 2 10 10 2 6 7 3 14 11 2 2 6 13 5 11 12 7 9 12 9 12 13 13 9 10 10 12 15 9 14 15 9 2 6 7 15 13 5 9 14 13 3 12 7 13 3 4 15 7 3 12 11 6 9 4 13 1 10 14 5 1 12 3 0 14 15 9 10 6 11 2 6 15 15 7 11 6 7 7 7 15 3 0 8 4 3 14 9 2 10 14 1 14 9 2 8 0 6 5 1 2 6 9 10 4 3 8 2 2 4 13 11 0 4 11 6 15 3 0 2 4 9 6 11 8 2 4 13 1 0 6 5 13 11 8 4 3 10 4 7 15 1 14 3 8 6 9 0 8 8 12 1 6 15 9 0 10 0 0 0 14 11 12 5 15 9 2 12 9 8 4 11 12 15 7 5 13 7 1 4 1 12 11 10 14 9 12 3 8 12 11 4 15 1 14 5 7 9 6 15 15 9 8 14 3 0 10 4 5 1 6 15 15 13 11 2 4 7 7 15 13 15 15 3 0 4 15 1 4 1 4 15 7 3 14 7 7 9 6 15 11 8 2 8 2 4 9 2 8 6 15 15 7 15 13 5 3 12 13 1 4 3 6 7 11 12 5 13 5 11 2 6 15 1 0 8 14 13 9 8 12 7 1 0 14 13 9 14 1 10 2 6 1 14 13 1 6 1 2 8 10 6 1 10 10 0 12 11 14 1 0 12 1 12 11 2 12 15 1 10 4 1 10 12 5 15 15 3 2 6 7 13 5 13 11 6 11 14 9 6 1 8 0 6 11 12 1 4 5 11 14 9 8 10 14 13 3 14 7 13 5 3 4 7 3 12 15 7 9 2 0 12 7 7 13 11 6 5 9 12 1 6 1 6 3 12 15 11 10 0 12 7 13 13 3 2 8 12 5 3 14 15 7 3 14 7 13 11 4 7 5 5 13 9 4 11 6 5 3 12 1 2 0 12 7 15 13 1 14 15 13 5 13 1 12 3 10 14 11 8 2 8 2 6 1 12 11 6 5 9 2 10 4 5 7 9 14 3 0 0 8 12 7 7 13 15 13 5 9 10 4 9 0 10 8 6 15 13 11 14 15 9 10 8 14 13 15 11 10 14 9 14 9 8 12 15 11 8 8 10 12 15 13 13 13 15 11 12 9 12 11 14 13 9 14 11 10 8 14 15 9 10 12 15 15 9 14 Keep up the good work! Thanks for your submission!
题目WA了两次,一次是因为没有判断输入 0 的时候应该打开那面墙,自己考虑疏忽了,确实没想到,第二次是因为自己预料到了矩阵里面标记可能会溢出,自己有麻烦把 mat 改成了 int 型的。
以后所有的图都用 int 类型的。
PROB Ordered Fractions [ANALYSIS] ---- 题目的输出和答案是一样的,但是却告诉我WA,不知道为什么啊,本地运行没错,感觉是精度问题,改成了 long double 过了。。白白提交了7次。
以后用map对double排序真得注意!
map<long double,PII> mp; map<long double,PII>::iterator p; int n; int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } int main() { FOPENTI FOPENTO SCF(n); mp.clear(); FOR(i,1,n) F(j,i) { long double t = j*1.0/i; int gcc = gcd(i,j); mp[t] = make_pair(j/gcc,i/gcc); } for(p = mp.begin(); p != mp.end(); p++) { printf("%d/%d\n",p->second.first,p->second.second); } printf("1/1\n"); }
---
USER: Rain M [m3324631] TASK: frac1 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3184 KB] Test 2: TEST OK [0.000 secs, 3184 KB] Test 3: TEST OK [0.000 secs, 3184 KB] Test 4: TEST OK [0.000 secs, 3184 KB] Test 5: TEST OK [0.000 secs, 3052 KB] Test 6: TEST OK [0.000 secs, 3184 KB] Test 7: TEST OK [0.000 secs, 3184 KB] Test 8: TEST OK [0.000 secs, 3184 KB] Test 9: TEST OK [0.000 secs, 3184 KB] Test 10: TEST OK [0.011 secs, 3184 KB] Test 11: TEST OK [0.022 secs, 3448 KB] All tests OK. Your program ('frac1') produced all correct answers! This is your submission #8 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 1 ------- test 2 ---- 2 ------- test 3 ---- 4 ------- test 4 ---- 7 ------- test 5 ---- 10 ------- test 6 ---- 15 ------- test 7 ---- 24 ------- test 8 ---- 50 ------- test 9 ---- 75 ------- test 10 ---- 100 ------- test 11 ---- 160 Keep up the good work! Thanks for your submission!
a
Here's a super fast solution from Russ: We notice that we can start with 0/1 and 1/1 as our ``endpoints'' and recursively generate the middle points by adding numerators and denominators. 0/1 1/1 1/2 1/3 2/3 1/4 2/5 3/5 3/4 1/5 2/7 3/8 3/7 4/7 5/8 5/7 4/5 Each fraction is created from the one up to its right and the one up to its left. This idea lends itself easily to a recursion that we cut off when we go too deep. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> int n; FILE *fout; /* print the fractions of denominator <= n between n1/d1 and n2/d2 */ void genfrac(int n1, int d1, int n2, int d2) { if(d1+d2 > n) /* cut off recursion */ return; genfrac(n1,d1, n1+n2,d1+d2); fprintf(fout, "%d/%d\n", n1+n2, d1+d2); genfrac(n1+n2,d1+d2, n2,d2); } void main(void) { FILE *fin; fin = fopen("frac1.in", "r"); fout = fopen("frac1.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d", &n); fprintf(fout, "0/1\n"); genfrac(0,1, 1,1); fprintf(fout, "1/1\n"); }
PROB Sorting A Three-Valued Sequence [ANALYSIS] ---- 这个题目居然弄蒙我了
一开始弄的我很蒙,需要交换几次,没做过这个样的题,DP?图论?更不是,还是喜欢往以前自己做的题目套,就是一小步贪心,然后求和就行了。
一开始直接sort 一下,然后对位置不对的求总和 /2,WA,考试的时候想了想,想出来为啥了,不过数据比较难出。。
从一出现的位置先对 1 进行贪心,然后就只剩下 2 和 3 了。
analysis 最后一个和我想的差不多,早知道就顺着我的思路继续想下去了!
int a[MAXN]; int b[4]; int main() { FOPENTI FOPENTO int n;SCF(n);SET(b,0); FOR(i,1,n) SCF(a[i]),b[a[i]]++; int sum =0; FOR(i,1,b[1]) { if(a[i] == 3) { sum++; int temp = -1; for(int j = n;j>=1;j--) { if(a[j] == 1) { temp = j; break; } } swap(a[i],a[temp]); } else if(a[i] == 2) { sum++; int temp = -1; for(int j = n - b[3];j>b[1];j--) { if(a[j] == 1) { temp = j; break; } } if(temp == -1) { for(int j = n;j>(n-b[3]);j--) { if(a[j] == 1) { temp = j; break; } } } swap(a[i],a[temp]); } } FOR(i,b[1] + 1,b[2] + b[1]) { if(a[i] == 3) sum++; } PCFLN(sum); }
最后一个数据太长了,不贴了。
USER: Rain M [m3324631] TASK: sort3 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3052 KB] Test 2: TEST OK [0.000 secs, 3052 KB] Test 3: TEST OK [0.000 secs, 3052 KB] Test 4: TEST OK [0.000 secs, 3052 KB] Test 5: TEST OK [0.000 secs, 3052 KB] Test 6: TEST OK [0.000 secs, 3052 KB] Test 7: TEST OK [0.000 secs, 3052 KB] Test 8: TEST OK [0.000 secs, 3052 KB] All tests OK. Your program ('sort3') produced all correct answers! This is your submission #9 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 3 2 1 3 ------- test 2 ---- 2 1 1 ------- test 3 ---- 6 1 2 3 2 3 1 ------- test 4 ---- 9 2 2 1 3 3 3 2 3 1 ------- test 5 ---- 20 1 1 3 2 1 1 1 3 2 1 3 3 2 1 3 1 1 2 3 1 ------- test 6 ---- 50 1 1 1 3 1 3 2 1 2 3 3 1 3 2 1 1 2 3 2 2 3 3 2 3 2 2 3 1 1 1 1 1 1 1 3 1 1 2 2 3 1 1 3 3 2 1 1 1 3 2 ------- test 7 ---- 100 3 2 2 3 1 2 2 2 1 2 2 3 2 3 2 2 3 3 2 2 2 3 3 3 3 2 2 1 2 2 1 1 1 3 1 1 2 2 3 3 1 1 2 2 1 1 3 1 3 1 3 2 1 2 1 3 3 3 1 2 1 1 2 1 3 2 2 1 1 1 3 1 2 2 3 3 2 3 3 2 1 2 3 1 3 1 1 3 3 1 1 1 1 3 1 1 2 3 1 2
原本最后两个题目是没有思路的,想了一天有回来做,KO了
PROB Healthy Holsteins [ANALYSIS] ---- 这个题目我用的IDdfs
struct node { int grass[MAXN]; }Nde[MAXNT]; int n,m,depthMax,statusLens; int goal[MAXN],status[MAXNT]; bool isfind = 0,vis[MAXN]; int botemp[MAXN] ={0}; bool findAns() { bool isOK = true; F(j,n) { if(botemp[j] < goal[j]) { isOK = false; break; } } return isOK; } void print() { printf("%d",statusLens); F(i,statusLens) { printf(" %d",status[i]+1); } puts(""); } void dfs(int depth) { if(findAns()) { isfind = true; print(); return; } if(depth == depthMax) { return; } FOR(i,status[statusLens-1],m-1) { //应该从上一个起向后搜索,剪掉这个枝就过了 if(!vis[i]) { vis[i] = 1; status[statusLens++] = i; F(j,n){ botemp[j] += Nde[i].grass[j]; } dfs(depth + 1); statusLens--; F(j,n){ botemp[j] -= Nde[i].grass[j]; } vis[i] = 0; if(isfind) return; } } } void IDdfs() { depthMax=0; isfind=false; while(!isfind && depthMax < m) { statusLens = 0; depthMax++; dfs(0); } } int main() { FOPENTI FOPENTO SCF(n); F(i,n) SCF(goal[i]); SCF(m); F(i,m) F(j,n) { SCF(Nde[i].grass[j]); } IDdfs(); }
剪枝了一下,过了,终于明白了,根据解的结构来剪枝。
USER: Rain M [m3324631] TASK: holstein LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3048 KB] Test 2: TEST OK [0.000 secs, 3048 KB] Test 3: TEST OK [0.000 secs, 3048 KB] Test 4: TEST OK [0.000 secs, 3048 KB] Test 5: TEST OK [0.000 secs, 3048 KB] Test 6: TEST OK [0.000 secs, 3048 KB] Test 7: TEST OK [0.000 secs, 3048 KB] Test 8: TEST OK [0.000 secs, 3048 KB] Test 9: TEST OK [0.000 secs, 3048 KB] Test 10: TEST OK [0.032 secs, 3048 KB] All tests OK. Your program ('holstein') produced all correct answers! This is your submission #6 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 1 50 1 50 ------- test 2 ---- 3 100 200 300 3 99 199 299 2 2 2 1000 1000 1000 ------- test 3 ---- 4 1 1 1 1 4 1 1 0 0 1 0 1 0 0 1 0 1 0 0 0 1 ------- test 4 ---- 5 10 20 30 40 50 5 10 10 10 10 10 0 10 10 10 10 0 0 10 10 10 0 0 0 10 10 0 0 0 0 10 ------- test 5 ---- 8 10 10 10 10 10 10 10 10 7 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 100 100 100 100 100 100 0 0 5 5 5 5 5 5 10 0 5 5 5 5 5 5 0 10 ------- test 6 ---- 5 163 221 146 425 509 10 98 69 68 18 129 132 185 196 64 176 40 70 57 9 115 73 189 145 87 117 45 114 45 0 18 137 137 174 73 178 48 143 33 142 192 33 107 148 2 158 32 42 153 90 41 165 81 156 7 121 ------- test 7 ---- 15 335 425 380 283 513 140 360 349 505 187 358 309 485 495 190 10 123 137 194 60 137 89 153 122 115 198 47 76 38 62 112 31 105 1 155 93 25 74 15 177 191 146 32 47 115 7 116 72 139 64 112 39 173 33 61 118 119 136 32 132 100 37 143 7 159 27 44 170 158 71 72 160 125 56 155 28 4 31 77 184 26 185 184 77 69 159 130 154 44 31 154 87 41 171 91 126 92 108 197 145 87 8 189 64 92 93 197 197 199 16 16 6 181 113 150 27 57 146 54 41 44 61 48 48 22 181 121 124 164 138 94 124 61 191 151 102 9 14 36 15 179 7 87 179 131 20 31 51 198 128 108 198 3 164 32 27 41 42 179 147 169 168 97 122 156 183 ------- test 8 ---- 20 924 519 510 589 901 627 827 814 520 725 674 709 777 512 540 731 695 801 984 517 12 143 197 55 68 193 181 88 163 109 98 159 36 197 139 45 176 34 128 93 0 83 25 61 46 5 46 66 5 44 47 100 93 180 176 51 198 50 21 69 119 126 54 27 100 145 29 123 2 82 95 148 109 69 106 99 105 142 89 36 27 13 144 70 120 170 129 195 15 86 29 24 134 37 147 161 168 123 0 71 114 181 94 165 119 198 30 173 25 93 130 126 62 85 34 0 170 131 49 171 11 24 108 110 154 55 123 78 74 23 181 54 73 91 173 42 152 83 104 6 132 114 67 169 73 145 152 122 80 162 39 95 139 167 167 16 80 75 36 126 106 121 108 168 194 52 88 111 187 118 39 53 103 139 108 30 76 57 136 198 43 45 107 113 3 0 121 162 169 73 45 86 12 70 122 40 149 184 100 135 60 77 154 97 176 186 164 3 147 65 29 119 8 41 166 3 69 188 90 97 35 106 16 167 192 107 170 30 164 92 175 107 60 118 172 57 141 152 55 15 14 198 83 112 82 94 38 176 54 179 3 104 48 113 121 12 106 50 7 26 25 ------- test 9 ---- 25 325 197 200 241 175 134 182 166 146 96 51 178 71 191 37 2 196 76 160 134 383 203 120 447 143 15 119 150 191 103 8 185 79 85 16 87 2 107 50 163 179 88 14 132 65 156 138 171 28 249 290 1 129 116 25 38 12 182 43 165 120 12 24 165 171 128 117 162 93 101 36 104 157 95 49 45 108 167 12 61 28 15 36 146 136 152 33 130 60 36 133 170 182 101 22 46 289 31 177 16 40 35 76 67 75 35 128 115 73 155 24 179 193 6 43 101 7 162 13 25 144 114 73 5 167 108 115 164 0 148 79 169 125 169 158 47 66 61 87 94 179 148 190 126 69 21 250 4 53 94 160 84 176 8 48 139 26 53 23 187 30 118 13 176 86 145 80 170 19 4 66 171 104 159 134 37 161 37 196 23 39 81 71 197 23 191 154 74 163 139 72 114 60 57 73 21 117 184 45 92 178 178 112 160 182 179 84 176 126 112 132 159 189 35 153 187 108 18 191 143 41 150 194 76 27 166 61 118 68 89 60 1 146 178 178 112 1 183 75 139 180 187 12 42 181 50 17 9 11 123 64 110 177 48 127 134 24 125 85 116 46 46 141 18 79 81 124 105 9 125 149 53 95 84 197 136 149 3 83 146 66 24 187 170 144 180 17 81 102 98 160 91 15 20 109 193 92 43 116 51 106 142 155 177 199 105 155 133 85 148 173 16 166 119 78 149 173 74 14 42 65 125 6 154 193 159 191 108 55 103 25 91 52 151 145 100 175 34 58 159 37 125 119 166 160 87 163 106 151 36 75 71 77 134 49 45 57 191 22 76 193 181 30 199 60 51 180 144 65 136 187 170 108 152 157 76 51 88 172 129 20 157 163 156 56 128 24 135 137 26 41 141 90 79 16 180 2 123 127 162 64 ------- test 10 ---- 25 826 953 853 512 620 769 722 833 719 754 730 521 908 622 877 737 534 882 560 812 684 787 984 983 783 15 167 78 129 9 41 56 71 132 29 45 61 152 13 76 172 152 27 21 0 38 24 93 25 155 18 84 69 79 59 179 166 107 28 79 63 11 101 95 73 172 70 141 97 72 170 74 132 48 195 136 88 97 174 121 28 28 157 29 56 148 1 46 160 188 114 179 49 55 22 20 79 98 144 153 100 98 23 58 152 58 197 72 199 1 171 18 57 61 64 78 100 61 192 135 92 80 168 129 144 192 178 80 119 78 95 14 15 102 26 135 127 145 130 95 117 193 95 98 19 163 135 81 74 182 85 121 35 187 43 113 58 56 152 56 183 162 76 90 43 138 38 142 107 156 197 105 81 95 29 9 182 124 77 118 106 55 143 6 18 194 92 145 159 61 132 59 12 80 144 179 39 42 122 51 102 14 107 32 188 114 67 127 165 15 100 102 183 5 34 125 28 69 180 69 71 2 114 131 107 45 64 107 118 27 151 160 74 129 77 35 188 35 105 160 148 189 122 177 144 139 33 145 5 59 74 162 74 58 98 48 123 149 6 35 148 93 2 103 144 189 28 108 91 26 49 150 64 81 166 119 87 144 20 64 49 93 51 132 105 185 91 115 74 169 46 154 55 17 159 42 188 163 189 73 161 36 7 65 4 4 162 132 166 89 82 10 13 182 92 118 157 123 106 168 127 158 169 84 21 159 112 73 38 102 93 185 119 195 97 10 14 156 146 54 190 26 111 44 109 18 83 91 78 193 164 175 7 54 63 78 84 60 156 20 84 140 183 130 142 14 41 17 97 14 60 155 27 184 9 156 103 173 20 51 174 96 90 35 196 77 122 23 137 43 195 41 70 89 4 26 64 186 170 51 13 Keep up the good work! Thanks for your submission!
这个题目本来可以一次过掉的。。。
PROB Hamming Codes [ANALYSIS] ----- 枚举
估算一下数据量,2^8 ^2也不过几万的执行次数,枚举可以过掉。
int n,b,d; int Ans[MAXN]; bool isOK(int x) { int num = 0,c = x; while(c) { c &= (c-1); num++; } if(num >= d) return true; else return false; } int main() { FOPENTI FOPENTO SCFT(n,b,d); SET(Ans,0); int len = pow(2,b),kk=0; Ans[kk++] = 0; FOR(i,1,len) { bool flag = 1; F(j,kk) { if(!isOK(i^Ans[j])) { flag = 0; break; } } if(flag) Ans[kk++] = i; if(kk == n) break; } F(i,kk) { if(!(i%10)) { printf("%d",Ans[i]); } else if(i%10 == 9) { printf(" %d\n",Ans[i]); } else { printf(" %d",Ans[i]); } } if((kk-1) % 10 != 9) puts(""); // 取模运算的优先级比 - 高 }
优先级注意一下。。。还是不愿意自己测试数据!要改。
USER: Rain M [m3324631] TASK: hamming LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3084 KB] Test 2: TEST OK [0.000 secs, 3084 KB] Test 3: TEST OK [0.000 secs, 3084 KB] Test 4: TEST OK [0.000 secs, 3084 KB] Test 5: TEST OK [0.000 secs, 3084 KB] Test 6: TEST OK [0.000 secs, 3084 KB] Test 7: TEST OK [0.000 secs, 3084 KB] Test 8: TEST OK [0.000 secs, 3084 KB] Test 9: TEST OK [0.000 secs, 3084 KB] Test 10: TEST OK [0.000 secs, 3084 KB] Test 11: TEST OK [0.000 secs, 3084 KB] All tests OK. Your program ('hamming') produced all correct answers! This is your submission #5 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 16 7 3 ------- test 2 ---- 2 7 7 ------- test 3 ---- 3 6 4 ------- test 4 ---- 5 5 1 ------- test 5 ---- 10 8 4 ------- test 6 ---- 20 6 2 ------- test 7 ---- 32 5 1 ------- test 8 ---- 50 8 2 ------- test 9 ---- 60 7 2 ------- test 10 ---- 16 8 3 ------- test 11 ---- 15 8 4 Keep up the good work! Thanks for your submission!
这part还是有些比较好的题目,至少很多我都没思路,后来才想起来思路的。
小小总结一下自己对剪枝的认识:
- 根据解的结构,有些是不用遍历的解
- 如果相当于枚举排列数,这个题目可能很多的排列情况是重复或者多余的
其他的剪枝方法暂时没遇到。。。
pump up!