ACM Learning Process-Guangdong University of Technology 2016 final-online game e block water (maximum problem | | Dynamic planning)

Source: Internet
Author: User

Title Link:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4

This topic will naturally be considered to discuss the longest or shortest board.

The pen may be simulated, you will know, assuming the longest board isR,0and then+1The location is high for0of the board, then for[0, R-1]The longest board in theRR,RRto theRThis short should be a depth ofA[RR]of water cover. The same[0, Rr-1]The longest board in theRRR,RRRto theRRThis paragraph is supposed to beA[rrr]cover, and so on can be doneRthe first paragraph, the same as the latter paragraph.

With regard to the maximum value of this piece, you can use logn maintenance such as RMQ , the total complexity is nlogn. But considering that the intervals are [0, R] and [R, N+1] this. So it is easy to think of DP,p[0][i] represents the maximum value from 0 to I, p[1][i] represents the angle of the maximum value from I to the n+1 interval. And then two times the equation shifts. The total complexity is O (n).

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<vector>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN = 1e6+5;intN, A[MAXN];//rmq-st Algorithm//Efficiency Nlogn//Sany, note the difference between the interval [0, n-1] and [1, N]intma[maxn][ -];voidRMQ () {memset (MA,0,sizeof(MA));  for(inti =0; I <= n+1; ++i) ma[i][0] =i;  for(intj =1; (1&LT;&LT;J) <= n+2; ++j) for(inti =0; i+ (1&LT;&LT;J)-1<= n+1; ++i) {if(a[ma[i][j-1]] > a[ma[i+ (1<< (J-1))][j-1] ]) ma[i][j]= ma[i][j-1]; ElseMa[i][j]= ma[i+ (1<< (J-1))][j-1]; }}intQueryintLtintRT) {    intK =0;  while((1<< (k +1)) <= rt-lt+1) K++; if(A[ma[lt][k]] > a[ma[rt-(1&LT;&LT;K) +1][k]])returnMa[lt][k]; Else        returnMa[rt-(1&LT;&LT;K) +1][k];}voidinput () {scanf ("%d", &N); a[0] = a[n+1] =0;  for(inti =1; I <= N; ++i) scanf ("%d", &A[i]); RMQ ();} LL Cal (intLtintRtinth) {LL ans=0;  for(inti = lt; I <= RT; ++i) ans+ = Max (0, H-A[i]); returnans;}voidWork () {LL ans=0; intMid = Query (0, n+1), K, now; now=mid;  while(Now >0) {k= Query (0, now-1); Ans+ = Cal (k, now-1, A[k]); now=K; } Now=mid;  while(Now < n+1) {k= Query (now+1, n+1); Ans+ = Cal (now+1, K, a[k]); now=K; } cout<< ans <<Endl;}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times )        {input ();    Work (); }    return 0;}
View Code

ACM Learning Process-Guangdong University of Technology 2016 final-online game e block water (maximum problem | | Dynamic planning)

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.