cychester

Luogu1070-道路游戏-动态规划

Solution

用对角线的前缀和快速进行转移,复杂度$O(N^3)$, 洛谷神机太快了$N^3$都能过

然而正解是单调队列优化, 能优化到$O(N^2)$,然而我弱得什么都不会

 

Code

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define rd read()
 5 #define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
 6 #define per(i,a,b) for(register int i = (a); i >= (b); --i)
 7 using namespace std;
 8 
 9 const int N = 2e3 + 5;
10 
11 int n, m, p;
12 int a[N][N], sum[N][N], cost[N];
13 int f[N], ans;
14 
15 inline int read() {
16     int X = 0, p = 1; char c = getchar();
17     for(; c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1;
18     for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0';
19     return X * p;
20 }
21 
22 inline int ch(int x) {
23     return (x - 1 + n) % n + 1;
24 }
25 
26 int main()
27 {
28     n = rd; m = rd; p = rd;
29     rep(i, 1, n) rep(j, 1, m) a[i][j] = rd;
30     rep(j, 1, m) rep(i, 1, n) sum[i][j] = sum[ch(i - 1)][j - 1] + a[i][j];
31     rep(i, 1, n) cost[i] = rd;
32     memset(f, 128, sizeof(f));
33     f[1] = 0;
34     rep(j, 1, m) rep(i, 1, n) rep(k, 1, p) {
35         if(j + k > m + 1) break;
36         f[j + k] = max(f[j + k], f[j] + sum[ch(i + k - 1)][j + k - 1] - sum[ch(i - 1)][j - 1] - cost[i]);
37     }
38     rep(i, 1, m + 1) ans = max(ans, f[i]);
39     printf("%d\n", ans);
40 }
View Code

 

 

posted on 2018-09-03 16:18  cychester  阅读(238)  评论(0编辑  收藏  举报

导航