51nod 1215 width of the array &poj 2796 feel Good (monotonic stack)

Source: Internet
Author: User
Tags cmath

  The monotone stack is the classic operation of which interval is the most value for each number.

Throw the number one into the monotone stack, when it pops up [st[top-1]+1,i-1] This interval is the number that pops up to be the most-valued interval.

poj2796 pop up when the answer can be updated

#include <iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<cmath>#include<map>#definell Long Longusing namespacestd;Const intmaxn=500010, inf=1e9;intn,top,ansl,ansr;ll ans=-1;intSt[maxn],a[maxn];ll SUM[MAXN];voidReadint&k) {    intf=1; k=0;CharC=GetChar ();  while(c<'0'|| C>'9') c=='-'&& (f=-1), c=GetChar ();  while(c<='9'&&c>='0') k=k*Ten+c-'0', c=GetChar (); K*=F;}intMain () {read (n);  for(intI=1; i<=n;i++) read (A[i]), sum[i]=sum[i-1]+a[i];a[++n]=-1;  for(intI=1; i<=n;i++)    {         for(; top&&a[i]<=a[st[top]];top--) {ll Val=1ll*a[st[top]]* (sum[i-1]-sum[st[top-1]]); if(Val>ans) ans=val,ansl=st[top-1]+1, ansr=i-1; } st[++top]=i; } printf ("%lld\n%d%d", ANS,ANSL,ANSR); return 0;}
View Code

51nod1215 the sum of the contribution of the maximal value to the answer and the contribution of the minimum value to the answer.

  Obvious contribution for a[i]* (I-st[top]) * (st1[top]-st[top-1]);

#include <iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<cmath>#include<map>#definell Long Longusing namespacestd;Const intmaxn=500010, inf=1e9;intN,top1,top2;inta[maxn],b[maxn],st1[maxn],st2[maxn];ll mx,mn;voidReadint&k) {    intf=1; k=0;CharC=GetChar ();  while(c<'0'|| C>'9') c=='-'&& (f=-1), c=GetChar ();  while(c<='9'&&c>='0') k=k*Ten+c-'0', c=GetChar (); K*=F;}intMain () {read (n);  for(intI=1; i<=n;i++) read (A[i]), b[i]=a[i];b[++n]=inf;  for(intI=1; i<=n;i++)    {         for(; top1&&a[i]<=a[st1[top1]];top1--) MN+=1ll*a[st1[top1]]* (I-ST1[TOP1]) * (st1[top1]-st1[top1-1]);  for(; top2&&b[i]>=b[st2[top2]];top2--) MX+=1ll*b[st2[top2]]* (I-ST2[TOP2]) * (st2[top2]-st2[top2-1]); st1[++top1]=st2[++top2]=i; } printf ("%lld\n", mx-mn); return 0;}
View Code

51nod 1215 width of the array &poj 2796 feel Good (monotonic stack)

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.