P2947 [USACO09MAR]仰望Look Up

    • 74通过
    • 122提交
  • 题目提供者洛谷OnlineJudge
  • 标签USACO2009云端
  • 难度普及/提高-
  • 时空限制1s / 128MB

提交  讨论  题解

最新讨论更多讨论

  • 中文翻译应当为向右看齐
  • 题目中文版范围。。

题目描述

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>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n,h[],ans[],stk[],top,num[]; int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &h[i]); for (int i = n; i >= ; i--)
{
while (top != && stk[top] <= h[i])
top--;
ans[i] = num[top];
stk[++top] = h[i];
num[top] = i;
}
for (int i = ; i <= n; i++)
printf("%d\n", ans[i]); return ;
}

最新文章

  1. IntelliJ IDEA中使用综合使用Maven和Struts2
  2. mac平台scala开发环境搭建
  3. Mongoose 是什么?
  4. [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
  5. framework层和native层实现联网控制(iptable方式)
  6. HDU3988-Harry Potter and the Hide Story(数论-质因数分解)
  7. arcgis api for js入门开发系列十二地图打印(GP服务)
  8. 聊一聊C# 8.0中的await foreach
  9. SoapUI简介及下载地址
  10. Redis 事物
  11. Spring Boot自动扫描
  12. [HDU3726]Graph and Queries
  13. Git从零开始(一)
  14. 20172308 实验四《Java面向对象程序设计 》实验报告
  15. centos7安装debuginfo
  16. Haskell语言学习笔记(32)Data.Maybe, Data.Either
  17. JAVA并发编程学习笔记------结构化并发应用程序
  18. DeveloperAppleHelp
  19. 腾讯云-搭建 FTP 文件服务
  20. Databases Questions &amp; Answers

热门文章

  1. vue中v-show和v-if的异同
  2. XCode5 使用AutoLayout情况下改变控件的 方法
  3. Dart Socket 与Java Socket连接
  4. 在Linux下搜索文件
  5. 本地Navicat连接虚拟机MySQL
  6. 2 &gt; 1 and 3 &lt; 4 or 4 &gt; 5 and 2 &lt; 1
  7. python列表中的赋值与深浅拷贝
  8. Problem I. Count - HDU - 6434(欧拉函数)
  9. shell脚本入门基础知识
  10. BZOJ 1222: [HNOI2001]产品加工