题目描述

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 <cstdio>
#define N 100005
int a[N],num[N],ans[N],n,last,stack[N],top;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);num[i]=i;
if(!top) {stack[++top]=i;continue;}
else if(a[stack[top]]<a[i])
{
for(;(a[stack[top]]<a[i])&&top;top--)
ans[num[stack[top]]]=i;
}
stack[++top]=i;
}
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}

最新文章

  1. VS2015墙内创建ionic2 【利用nrm更换源,完美!】
  2. Maplace.js – 小巧实用的 jQuery 谷歌地图插件
  3. leetcode 179. Largest Number 求最大组合数 ---------- java
  4. .NET Core扩展IServiceCollection自动注册服务
  5. Android Layout XML属性
  6. 2.js基础
  7. css select 样式列表-----另一种样式列表
  8. 通过 CALayer 修改 UIImageView 的界面属性
  9. web.config配置详细说明
  10. iOS绘制view
  11. web开发 - 从零开始 - 02 - 常用标签
  12. 给节点设置tag【从零開始cocos3.0final 】
  13. 环信 之 iOS 客户端集成一:导入库
  14. 201521123062《Java程序设计》第5周学习总结
  15. Python logger 没打出行数
  16. python模块之sniffio
  17. Linux 使用tty0 显示10分钟自动关闭功能
  18. DHTMLX学习总结
  19. css3的overflow-anchor
  20. 爬虫进阶教程:极验(GEETEST)验证码破解教程

热门文章

  1. c/c++面试23-29
  2. ecplise常见的一些问题
  3. 执行多个Sql脚本,Sqlplus
  4. 《剑指offer》面试题15—输出链表中倒数第n个结点
  5. sql #与$的区别
  6. 【WIP】Swift4 闭包
  7. Linux命令之清空当前文件
  8. C 语言实例 - 判断元音/辅音
  9. Java | 基础归纳 | 随机数应用
  10. 未知宽高div水平垂直居中的3种方法