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 }
  • 相关阅读:
    AtCoder Grand Contest 031
    CF1010D Mars rover
    51Nod 1317 相似字符串对
    upd
    查漏补缺——字符串www.qq.com所有非空子串
    c语言查漏补缺——Win32环境下动态链接库(DLL)编程原理
    编程——二维矩阵中1所构成的块个数(孤岛问题)
    使用Windows自带远程桌面应用连接CentOS8远程桌面
    ZeroTier + NoMachine
    WinPE装入硬盘做应急系统教程
  • 原文地址:https://www.cnblogs.com/cszlg/p/3024404.html
Copyright © 2011-2022 走看看