POJ 3017 Monotone Queue

Source: Internet
Author: User

Can only say that the code is beautiful as a picture. (Of course I mean the inner logic is not my Code style, captain see to understand ha, ah haha haha haha)

Normal thinking, F[i]=f[j]+max (A[j+1],a[j+2]....a[i]), enumeration J, obviously hard to time out, so need to have a monotone queue to always get the maximum value as soon as possible and need some pruning;

The pruning condition has two, one is and cannot exceed M, one is obviously f[i] is a non-strict increment sequence, according to these two conditions pruning;

Then set up a monotone queue, each time I insert a new I will be preceded by the first and less than equal to the conditions of M, and then for J<i, if A[j]<a[i] can throw J, then obviously the newly added elements will become the smallest of the monotone queue, the monotone ranks as descending sequence.

And then just look at the code.

1#include <cstdio>2#include <cstring>3#include <Set>4#include <iostream>5#include <map>6#include <math.h>7#include <algorithm>8#include <vector>9 using namespacestd;Ten  OnetypedefLong Longll; A  - Const intn=100005; - Const Long Longinf=1000000000LL; the intN; - ll M; - ll A[n],f[n]; - ll Solve () { + inti,j,k=0, fg=0, l=0, r=0; - intQ[n]; +ll sum=0; A  for(i=1; i<=n;i++){ atsum+=A[i]; -      while(sum>m) sum-=a[++K]; -     if(k>=i) { -fg=1; -          Break; -     } in     while(L<=r&&a[q[r]]<=a[i]) r--;//the equals sign is the detail . -q[++r]=i; to     while(l<=r&&q[l]<=k) l++; + ll tmp; -    intkk=K; thef[i]=INF; *         //cout<<l<< "" <<r<< "" <<kk<<endl; $          for(j=l; j<=r; j + +)//the queue-optimized DP process ensures that the elements in the monotone queue are decrementedPanax Notoginseng         { -tmp=f[kk]+A[q[j]]; the           //cout<<tmp<< "<<f[i]<<" "<<i<<" "<<kk<<endl; +             if(Tmp<f[i]) f[i]=tmp; AKK=Q[J];//KK indicates that the current maximum value exists in the range of [Kk+1,i] the         } + //f[i]=tmp; - //cout<<f[i]<< "" <<i<<endl; $ } $ if(FG) -     return-1; - Else the     returnF[n]; - }Wuyi intMain () the { -Freopen ("In.txt","R", stdin); WuCin>>n>>m; -    inti; About     for(i=1; i<=n;i++) $scanf"%lld",&a[i]); -Cout<<solve () <<Endl; -     return 0; -}

POJ 3017 Monotone Queue

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.