BZOJ3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 87 Solved: 58
[Submit][Status]
Description
约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j. 求出每只奶牛离她最近的仰望对象.
Input
第1行输入N,之后每行输入一个身高.
Output
共N行,按顺序每行输出一只奶牛的最近仰望对象.如果没有仰望对象,输出0.
Sample Input
6
3
2
6
1
1
2
3
2
6
1
1
2
Sample Output
3
3
0
6
6
0
3
0
6
6
0
HINT
Source
题解:
裸单调栈,呵呵
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 100000+5 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define mod 1000000007 23 using namespace std; 24 inline int read() 25 { 26 int x=0,f=1;char ch=getchar(); 27 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 28 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 29 return x*f; 30 } 31 int n,top,a[maxn],b[maxn],sta[maxn]; 32 int main() 33 { 34 freopen("input.txt","r",stdin); 35 freopen("output.txt","w",stdout); 36 n=read(); 37 for1(i,n)a[i]=read(); 38 for1(i,n) 39 { 40 while(top&&a[i]>a[sta[top]])b[sta[top--]]=i; 41 sta[++top]=i; 42 } 43 for1(i,n)printf("%d\n",b[i]); 44 return 0; 45 }