猴子向右看,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:

  • Line 1: A single integer: N
  • Lines 2..N+1: Line i+1 contains the single integer: Hi

  • 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

    6326112

    Sample Output

    330660

    Hint

    Monkey 1 and 2 both look up to monkey 3; monkey 4 and 5 both look up to monkey 6; and monkey 3 and 6 do not look up to any monkey.

    题意:告诉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;}





    0 0
    原创粉丝点击
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 浦发信用卡卡片名字印错了怎么办 公主工作很辛苦坚持不下去怎么办 在表格里怎么办名字转换成拼音 激素脸有黑头毛孔大该怎么办 兢兢业业上班但不招领导喜欢怎么办 身体长的还算苗条但就屁股大怎么办 我想学英语从基础开始要怎么办 政府单位领导给我调岗我该怎么办 领导在单位想捞钱我该怎么办 单位领导是宵小之人我该怎么办 一件事想不明白非得想明白怎么办 在四楼上课时发生地震该怎么办 媳妇要离婚我想要孩子该怎么办 媳妇带了避孕环我想要孩子怎么办 新开的文具店一点生意都没有怎么办 孩子在学校被坏孩子欺负了该怎么办 老师像个傻叉我妈还喷我我怎么办啊 承台上预埋桥墩连接钢筋错了怎么办 冲床油缸螺栓拆不下来怎么办 汇款到账银行写错了怎么办 搜狗输入法数字序号超过20怎么办 苹果手机保存的图片变模糊怎么办 微信视频保存到手机变模糊怎么办 自己的位置被别人取代了怎么办 给工厂做半成品老板跑了怎么办 微信变成英文再恢复汉字怎么办 cad中标注尺寸数字太小怎么办 扣扣的钱包手势密码忘记了怎么办 台式电脑带符号的数字打不出怎么办 情侣之间出现看见对方就烦怎么办 电信卡号和联通卡号怎么办情侣号 电脑能登qq但打不开网页怎么办 想跟朋友聊天但对方不理怎么办 刚进婆家门被婆婆欺负怎么办 支付宝的聊天记录被删了怎么办 彩票站买彩票把钱付了没出票怎么办 与异性朋友聊天没话题了怎么办 快递写错地址但已经发货了怎么办 快递写错电话但已经发货了怎么办 微信添加好友功能被限制怎么办 qq号被冻结了限制解封怎么办