It is easy to think of dp[i][j] to indicate the maximum happy value at the beginning of the J junction of Line I.
Each time you can go left, or go to the right, and then go north. (or go directly to the north)
Go to left, state transfer to DP[I][J] = Dp[i][k] + happy[i][k][j] (for ease of handling, I start numbering from 1, 0 rows DP value 0)
Processing out prefixes and, happy[i][k][j] expressed as sum[i][j]-sum[i][k]
Go left should take Max (Dp[i][k]-sum[i][k])
K should meet Time[i][k][j] <= K
As J changes the range of K is a sliding window, which is maintained with a monotone queue.
Right to move Dp[i][k] + sum[i][k]-sum[i][j], similar to processing.
Transfer to O (1), Complexity of O (n*m)
#include <bits/stdc++.h>using namespacestd;Const intN =102, M = 1e4+5;intN, M, K;intH[n][m], t[n][m];intDp[n][m];intQ[m];//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif while(SCANF ("%d%d%d", &n, &m, &k), n+m+k) {N++; for(inti =1; I <= N; i++){ for(intj =1; J <= M; j + +) {scanf ("%d", h[i]+j); H[I][J]+ = h[i][j-1]; } } for(inti =1; I <= N; i++){ for(intj =1; J <= M; j + +) {scanf ("%d", t[i]+j); T[I][J]+ = t[i][j-1]; } } for(inti =1; I <= N; i++){ #defineMaintain (diff) while (Hd<rl && diff (J,q[hd]) > K) hd++;#defineInst (val) while (Hd<rl && val (q[rl-1)) <= Val (j)) rl--; q[rl++] = j;intHD =0, RL =0; #defineDis (x) (T[i][x])#defineLval (x) (Dp[i-1][x]-h[i][x])#defineLdis (j,k) Dis (j)-dis (k) for(intj =0; J <= M; J + +) {Inst (lval) Maintain (Ldis) Dp[i][j]= Lval (Q[HD]) +H[i][j]; } HD= RL =0; #defineRval (x) (Dp[i-1][x] + h[i][x])#defineRDIs (j,k) dis (k)-dis (j) for(intj = m; J >=0; j--) {Inst (rval) Maintain (RDIs) Dp[i][j]= Max (Dp[i][j],rval (Q[HD])-H[i][j]); } } intAns =0; for(intj =0; J <= M; J + +) {ans=Max (ans,dp[n][j]); } printf ("%d\n", ans); } return 0;}
UVA Live achrive 4327 Parade (monotone queue, DP)