题目描述

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

第 1 行输入 N,之后每行输入一个身高 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.

共 N 行,按顺序每行输出一只奶牛的最近仰望对象,如果没有仰望对象,输出 0。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int a[N],q[N],ans[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
q[1]=1;
int l=1,r=1;
for(int i=1;i<=n;i++){
while(r>=l&&a[q[r]]<a[i]){
ans[q[r]]=i;
r--;
}
q[++r]=i;
}
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
}

最新文章

  1. 移动端API架构 统一Proxy还是各自为政?
  2. How to Prevent Cross-Site Scripting Attacks
  3. c3p0数据库连接池使用
  4. 常见HTTP状态(304,200等)
  5. DDD学习笔记一
  6. 前端学习:html基础学习三
  7. 30分钟了解Springboot整合Shiro
  8. LinkedHashMap 根据PUT顺序排序Map
  9. 前后端分离djangorestframework——视图组件
  10. C# 中那些常用的工具类(Utility Class)(二)
  11. NLTK的安装
  12. Git基础(四) 查看已暂存和未暂存的修改
  13. 关于asp.net mvc中的httpModules 与 httpHandler
  14. URL diff URI
  15. [asp.net]C#实现json的序列化和反序列化
  16. SQL语句小tips(持续更新)
  17. JS 中的布尔运算符 &amp;&amp; 和 ||
  18. bzoj 4237: 稻草人 -- CDQ分治
  19. 创建操作表(UIActionSheet)
  20. 四大VDI客户端 总有一款适合你

热门文章

  1. 爬虫学习--Urllib库基本使用 Day1
  2. python_day2(列表,元组,字典,字符串)
  3. thinkphp 获取前端传递过来的参数
  4. 用maven创建web项目(spring Mvc)
  5. NetCore基于EasyNetQ的高级API使用RabbitMq
  6. 力扣(LeetCode)删除排序链表中的重复元素II 个人题解
  7. JS的原型和继承
  8. React动画库
  9. java多线程,多线程加锁以及Condition类的使用
  10. code migrate