zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    使用freemarker生成word,步骤详解并奉上源代码
    汉诺塔问题
    java interview
    java 反射
    java 匿名内部类
    java 内部类(转)
    MYSQL和ORACLE的一些区别
    Hibernate总结(转)
    Hibernate的使用
    Arduino LM35温度检测
  • 原文地址:https://www.cnblogs.com/cszlg/p/3024404.html
Copyright © 2011-2022 走看看