标签:
根本想不出来。。。
原来还是暴力出奇迹啊QAQ
无限ymymym中
1 /************************************************************** 2 Problem: 4007 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:240 ms 7 Memory:13216 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 #include <algorithm> 13 14 using namespace std; 15 const int N = (1 << 10) + 5; 16 17 int n, m, ans; 18 int a[N][N], b[N][N], f[N][N]; 19 int now[N]; 20 21 inline int read(); 22 23 #define ls p << 1 24 #define rs p << 1 | 1 25 void dfs(int p, int sz) { 26 int i, j, mnl, mxl, tot; 27 if (sz == 1) { 28 f[p][0] = f[p][1] = 0; 29 for (i = p >> 1; i; i >>= 1) 30 if (now[i] == 0) f[p][1] += a[p][i]; 31 else f[p][0] += b[p][i]; 32 return; 33 } 34 tot = min(sz, m); 35 memset(f[p], 0, sizeof(f[0][0]) * (tot + 1)); 36 now[p] = 0; 37 dfs(ls, sz >> 1), dfs(rs, sz >> 1); 38 for (i = 0; i <= tot; ++i) { 39 mnl = max(i - (sz >> 1), 0), mxl = min(sz >> 1, i); 40 for (j = mnl; j <= mxl; ++j) 41 f[p][i] = max(f[p][i], f[ls][j] + f[rs][i - j]); 42 } 43 44 now[p] = 1; 45 dfs(ls, sz >> 1), dfs(rs, sz >> 1); 46 for (i = 0; i <= tot; ++i) { 47 mnl = max(i - (sz >> 1), 0), mxl = min(sz >> 1, i); 48 for (j = mnl; j <= mxl; ++j) 49 f[p][i] = max(f[p][i], f[ls][j] + f[rs][i - j]); 50 } 51 } 52 #undef ls 53 #undef rs 54 55 int main() { 56 int i, j; 57 n = read(), m = read(); 58 for (i = 1 << n - 1; i < 1 << n; ++i) 59 for (j = i >> 1; j; j >>= 1) 60 a[i][j] = read(); 61 for (i = 1 << n - 1; i < 1 << n; ++i) 62 for (j = i >> 1; j; j >>= 1) 63 b[i][j] = read(); 64 dfs(1, 1 << n - 1); 65 for (ans = i = 0; i <= m; ++i) 66 ans = max(ans, f[1][i]); 67 printf("%d\n", ans); 68 return 0; 69 } 70 71 inline int read() { 72 static int x; 73 static char ch; 74 x = 0, ch = getchar(); 75 while (ch < ‘0‘ || ‘9‘ < ch) 76 ch = getchar(); 77 while (‘0‘ <= ch && ch <= ‘9‘) { 78 x = x * 10 + ch - ‘0‘; 79 ch = getchar(); 80 } 81 return x; 82 }
标签:
原文地址:http://www.cnblogs.com/rausen/p/4448810.html