UVA Live achrive 4327 Parade (monotone queue, DP)

Source: Internet
Author: User
Tags define local

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.