洛谷1005(dp)

1.不要贪,缩小区间去dp就好。

2.预处理指数。

3.__int128可还行。

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 template <typename T> void read(T &x) {
 9     x = 0;
10     int s = 1, c = getchar();
11     for (; !isdigit(c); c = getchar())
12         if (c == '-')    s = -1;
13     for (; isdigit(c); c = getchar())
14         x = x * 10 + c - 48;
15     x *= s;
16 }
17 
18 template <typename T> void write(T x) {
19     if (x < 0)    x = -x, putchar('-');
20     if (x > 9)    write(x / 10);
21     putchar(x % 10 + '0');
22 }
23 
24 template <typename T> void writeln(T x) {
25     write(x);
26     puts("");
27 }
28 
29 const int maxn = 81;
30 
31 __int128 f[maxn][maxn], p[maxn], num[maxn], ans;
32 
33 __int128 dp(int l, int r, int depth) {
34     if (l == r)    f[l][r] = num[l] * p[depth];
35     if (f[l][r] >= 0)    return f[l][r];
36     return    f[l][r] = max(num[l] * p[depth] + dp(l + 1, r, depth + 1), num[r] * p[depth] + dp(l, r - 1, depth + 1));
37 }
38 
39 int main() {
40     int n, m;
41     read(n), read(m);
42     p[0] = 1;
43     for (int i = 1; i <= m; i++)
44         p[i] = p[i - 1] * 2;
45     for (int i = 1; i <= n; i++) {
46         for (int j = 1; j <= m; j++)
47             read(num[j]);
48         memset(f, -1, sizeof f);
49         ans += dp(1, m, 1);
50     }
51     writeln(ans);
52     return 0;
53 }
原文地址:https://www.cnblogs.com/AlphaWA/p/10527909.html