您的位置:首页 > Web前端

单调栈经典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)

2015-07-25 03:06 330 查看
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
stack<int> stk;
int a[maxn] , dpl[maxn] , dpr[maxn] , ans[maxn];
int main() {
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= n; i++)
while(!stk.empty() && a[stk.top()] >= a[i]) stk.pop();
if(stk.empty()) dpl[i] = 1;
else dpl[i] = stk.top()+1;
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i >= 1; i--) {
while(!stk.empty() && a[stk.top()] >= a[i]) stk.pop();
if(stk.empty()) dpr[i] = n;
else dpr[i] = stk.top()-1;
stk.push(i);
}
/*memset(ans , 0 , sizeof(int)*(n+1));
for(int i = 1; i <= n; i++) ans[dpr[i]-dpl[i]+1] = max(ans[dpr[i]-dpl[i]+1] , a[i]);
for(int i = n-1; i >= 1; i--) ans[i] = max(ans[i] , ans[i+1]);
for(int i = 1; i <= n; i++) printf("%d%c",ans[i]," \n"[i == n]);*/
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: