Look Up

Problem 1845 Look Up

Accept: 173    Submit: 543
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

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

6 3 2 6 1 1 2

 Sample Output

3 3 0 6 6 0

 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.

 Source

Funny Programming Contest -- OSUM

 

 
用优先队列保存输入信息。
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stack>
 4 #include <queue>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <cstring>
 8 using namespace std;
 9 
10 struct Height
11 {
12     int h, id;
13 }hei[100005];
14 int ans[100005], n;
15 priority_queue<Height> Q;
16 
17 bool operator < (const Height& x, const Height& y)
18 {
19     return x.h > y.h;
20 }
21 
22 int main()
23 {
24     while(scanf("%d", &n) != EOF)
25     {
26         for(int i = 0; i < n; i++)
27         {
28             scanf("%d", &hei[i].h);
29             hei[i].id = i;
30             while(!Q.empty() && hei[i].h > (Q.top()).h)
31             {
32                 ans[(Q.top()).id] = i + 1;
33                 Q.pop();
34             }
35             Q.push(hei[i]);
36         }
37         while(!Q.empty())
38         {
39             ans[(Q.top()).id] = 0;
40             Q.pop();
41         }
42         for(int i = 0; i < n; i++) printf("%d\n", ans[i]);
43     }
44     return 0;
45 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值