猴子向右看,FOJ上某题 单调栈
来源:互联网 发布:lol徐老师淘宝店多少 编辑:程序博客网 时间:2024/05/17 06:30
N (1 <= N <= 100,000) monkeys in the mountains, conveniently numbered 1..N, are once again standing in a row. Monkey i has height Hi (1 <= Hi <= 1,000,000).
Each monkey is looking to his left toward those with higher index numbers. We say that monkey i "looks up" to monkey j if i < j and Hi< Hj. For each monkey i, we would like to know the index of the first monkey in line looked up to by monkey i.
Input
Input consists of several testcases. The format of each case as follow:
Output
For each testcase, output N lines. Line i contains a single integer representing the smallest index of a monkey up to which monkey i looks. If no such monkey exists, print 0.
Sample Input
Sample Output
Hint
题意:告诉n个一排猴子,每个猴子向右看,问第一个比自己高的猴子的位置,没有就输出0,最后一个也是0
#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <stack>#define N 100009using namespace std;int a[N];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } int ans[N]; memset(ans,0,sizeof ans); ans[n]=n;//初始化为n,但是实际值为0 for(int i=n-1;i>=1;i--)//找右边第一个比a[i]小的位置 { int tt=i; while(tt<n&&a[i]>=a[tt+1]) tt=ans[tt+1]; ans[i]=tt; } for(int i=1;i<n;i++) { if(ans[i]==n)//当是最后一个猴子的时候,并不一定保证比当前猴子高 if(a[i]<a[n]) printf("%d\n",ans[i]+1); else cout<<0<<endl; else printf("%d\n",ans[i]+1); } cout<<0<<endl; } return 0;}
- 猴子向右看,FOJ上某题 单调栈
- FOJ 2136 取糖果(单调栈)
- 单调队列经典题目 FOJ 1894
- foj Problem 1894 志愿者选拔---单调队列
- FOJ 1894 志愿者选拔 单调队列
- 看《浪漫向左,婚姻向右》的收获
- 看这个70事情向右得知荷
- FOJ 1914. Funny Positive Sequence 【单调队列优化】
- FOJ 题目1894 志愿者选拔 (单调队列)
- 算法思想——单调队列(Foj志愿者问题)
- 理解c语言中的复杂声明-向右看,向左看
- 单调栈 单调队列
- 看收购:百度向左,腾讯向右,阿里犹豫
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- block传值的简单使用
- main函数被调用时传递的三个参数
- 86.求出分数前n项和
- Binary Tree Right Side View
- 树状数组入门
- 猴子向右看,FOJ上某题 单调栈
- [转]短信验证码如何防止不恶意点击被刷!
- Cannot run program "XXXXX/android-sdks/build-tools/aapt.exe": error=2,
- TP接口token
- MYSQL启动后报:ERROR! The server quit without updating PID file错误的问题解决
- leetcode Two Sum 哈希表
- VS2010+QT5编程之巧妙运用定时器解决QTreeView/QTableView等QAbstractItemView实时条目统计问题
- OpenMAX/IL: OMX IL 学习笔记【1】- 接口与头文件
- Lex 和 Yacc 编程环境配置(Parser Generator2 + VC6.0 配置)