bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e4+5;
int n,h[N],c[N],q[N],he,tl,cnt[N],ans;
int rdn()
{
  int ret=0;bool fx=1;char ch=getchar();
  while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
  while(ch>='0'&&ch<='9') ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
  return fx?ret:-ret;
}
int main()
{
  n=rdn();for(int i=1;i<=n;i++)h[i]=rdn(),c[i]=rdn();
  he=1;
  for(int i=1;i<=n;i++)
    {
      while(he<=tl&&h[i]>h[q[tl]])cnt[i]+=c[q[tl]],tl--;
      q[++tl]=i;
    }
  he=1;tl=0;
  for(int i=n;i;i--)
    {
      while(he<=tl&&h[i]>h[q[tl]])cnt[i]+=c[q[tl]],tl--;
      q[++tl]=i;
    }
  for(int i=1;i<=n;i++)ans=max(ans,cnt[i]);
  printf("%d\n",ans);
  return 0;
}

 

posted on 2018-10-27 09:06  Narh  阅读(123)  评论(0编辑  收藏  举报

导航