P2947 [USACO09MAR]仰望Look Up
来源:互联网 发布:网络教育官网 编辑:程序博客网 时间:2024/04/25 15:49
题目描述
Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again standing in a row. Cow i has height H_i (1 <= H_i <= 1,000,000).
Each cow is looking to her left toward those with higher index numbers. We say that cow i 'looks up' to cow j if i < j and H_i < H_j. For each cow i, FJ would like to know the index of the first cow in line looked up to by cow i.
Note: about 50% of the test data will have N <= 1,000.
约翰的N(1≤N≤10^5)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j. 求出每只奶牛离她最近的仰望对象.
Input
输入输出格式
输入格式:Line 1: A single integer: N
- Lines 2..N+1: Line i+1 contains the single integer: H_i
- Lines 1..N: Line i contains a single integer representing the smallest index of a cow up to which cow i looks. If no such cow exists, print 0.
输入输出样例
输入样例#1:6
3
2
6
1
1
2
输出样例#1:
3
3
0
6
6
0
说明
FJ has six cows of heights 3, 2, 6, 1, 1, and 2.
Cows 1 and 2 both look up to cow 3; cows 4 and 5 both look up to cow 6; and cows 3 and 6 do not look up to any cow.
题解:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000+5;typedef long long ll;int read(){int x=0,f=1;char ch;ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}return x*f;}int n;int s[maxn],h[maxn],a[maxn];int main(){int i,top=1;n=read();h[1]=read();s[1]=1;for(i=2;i<=n;i++){h[i]=read();while(top&&h[i]>h[s[top]]) {a[s[top]]=i;top--;}s[++top]=i;}for(i=1;i<=n;i++)printf("%d\n",a[i]);return 0;}
- P2947 [USACO09MAR]仰望Look Up
- bzoj3401[Usaco2009 Mar]Look Up 仰望
- [BZOJ3401] [Usaco2009 Mar]Look Up 仰望
- 洛谷 2947 仰望Look Up(单调栈)
- BZOJ 3401: [Usaco2009 Mar]Look Up 仰望
- bzoj 3401: [Usaco2009 Mar]Look Up 仰望
- bzoj3401 [Usaco2009 Mar]Look Up 仰望
- [bzoj3401][Usaco2009 Mar]Look Up 仰望
- 【BZOJ3401】[Usaco2009 Mar]Look Up 仰望【单调栈】
- [BZOJ3401][Usaco2009 Mar]Look Up 仰望(单调栈)
- bzoj3401 [Usaco2009 Mar]Look Up 仰望 单调栈
- 【BZOJ3401】【单调栈】[Usaco2009 Mar]Look Up 仰望 题解
- LDAP Look up
- FOJ 1845 Look Up
- dns look up command
- DTC look up 例子
- 低头族,Look Up!
- 什么是LUT(LOOK UP TABLE)
- PHP文件下载时乱码解决
- CentOS6.9+Mysql5.7.18源码安装
- Liunx进程间IPC通信—消息队列
- 二叉树的链表实现
- xamarin学习笔记A06(安卓RecycleView点击事件)
- P2947 [USACO09MAR]仰望Look Up
- linux中makefile里的$(@:_config=)的解释
- Android拖拽动画实现
- Unity 播放 带 alpha 通道的视频(Video Player组件)
- Android 神兵利器Dagger2使用详解(四)Scope注解的使用及源码分析
- 学习linux第一步,关闭selinux防火墙
- javaIO流
- 关于C单链表的面试题(进阶题)
- String 堆内存和栈内存