hdu1506+ luogu 1440 单调栈/单调队列裸题

就是简单的记录一下代码,前几天写后缀数组+单调栈/单调队列写的有点傻....

首先是hdu1506单调栈:

#include<bits/stdc++.h>
#define ll long long
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
using namespace std;//head
const int maxn=1e6+10,maxm=2e6+10;
int casn,n,m,k;
ll a[maxn];
stack<int> stk;
int l[maxn],r[maxn];
int main() {
  while(cin>>n){
    if(n==0) break;
    rep(i,1,n) cin>>a[i];
    ll ans=a[1];
    while(!stk.empty()) stk.pop();
    rep(i,1,n){
      while(!stk.empty()&&a[stk.top()]>=a[i]) stk.pop();
      if(!stk.empty()) l[i]=stk.top()+1;
      else l[i]=1;
      stk.push(i);
    }
    while(!stk.empty()) stk.pop();
    per(i,1,n){
      while(!stk.empty()&&a[stk.top()]>=a[i]) stk.pop();
      if(!stk.empty()) r[i]=stk.top()-1;
      else r[i]=n;
      stk.push(i);
    }
    rep(i,1,n) {
      ans=max(ans,a[i]*(r[i]-l[i]+1));
    }
    cout<<ans<<endl;
  }
  return 0;
}

 然后是p1440

#include<bits/stdc++.h>
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;//head
const int maxn=1e7+10,maxm=2e6+10;
int casn,n,m,k;
int a[maxn],ans[maxn];
int main() {
  deque<int> q;
  cin>>n>>m;
  rep(i,1,n) cin>>a[i];
  rep(i,1,n){
    if(q.empty()) ans[i]=0;
    else ans[i]=a[q.front()];
    if(!q.empty()&&i-q.front()+1>m) q.pop_front();
    while(!q.empty()&&a[i]<a[q.back()]) q.pop_back();
    q.push_back(i);
  }
  rep(i,1,n) cout<<ans[i]<<'\n';
  return 0;
}

 

posted @ 2019-08-31 01:10  nervending  阅读(229)  评论(0编辑  收藏  举报