【BZOJ3401】【单调栈】[Usaco2009 Mar]Look Up 仰望 题解

来源:互联网 发布:网络被攻击怎么办 编辑:程序博客网 时间:2024/04/27 06:54

Description

约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向后看齐.对
于奶牛i,如果奶牛j满足i

#include <bits/stdc++.h>#define LL long long#define clr(x) memset(x, 0, sizeof x)#define ms(a, x) memset(x, a, sizeof x)#define digit (ch <  '0' || ch >  '9')using namespace std;template <class T> inline void read(T &x) {    int flag = 1; x = 0;    register char ch = getchar();    while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }    while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }    x *= flag;}const int maxn = 1e5+5;int n,a[maxn],sta[maxn],top,ans[maxn];int main () {    read(n);    for(int i = 1; i <= n; i++) read(a[i]);    for(int i = n; i >= 1; i--) {        while(a[sta[top]] <= a[i] && top) top--;        ans[i] = sta[top]; sta[++top] = i;    }    for(int i = 1; i <= n; i++) printf("%d\n",ans[i]);    return 0;}
原创粉丝点击