zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #75 (Div. 1 Only) B. Queue 二分

    B. Queue

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    codeforces.com/problemset/problem/91/B

    Description

    There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

    The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.

    The airport manager asked you to count for each of n walruses in the queue his displeasure.

    Input

    The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).

    Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.

    Output

    Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

    Sample Input

    6
    10 8 5 3 50 45

    Sample Output

    2 1 0 -1 0 -1

    HINT

    题意

    给你一个数列,让你找到最右边比这个数小的数的位置,如果没有就输出-1

    题解:

    对与某一位i,假设最右边比他大的数为j,那么a[i]>a[j],然后将min{a[j+1],a[j+2],...,a[n]}记作min[j+1], 则a[i]<=min[j+1],

    不难想到不等式: min[j]<=a[j]<a[i]<=min[j+1]

    这就说明min数组单增,然后在单增序列里面找一个值。

    这不就是二分吗?然后就没了。

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 #define N 100050
     5 int n,a[N],mi[N];
     6 template<typename T>void read(T&x)
     7 {
     8   ll k=0; char c=getchar();
     9   x=0;
    10   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
    11   if (c==EOF)exit(0);
    12   while(isdigit(c))x=x*10+c-'0',c=getchar();
    13   x=k?-x:x;
    14 }
    15 void read_char(char &c)
    16 {while(!isalpha(c=getchar())&&c!=EOF);}
    17 int ef(int x,int l,int r)
    18 {
    19   if (l==r)return l;
    20   int mid=(l+r+1)>>1;
    21   if (x>mi[mid])
    22     return ef(x,mid,r);
    23   else return ef(x,l,mid-1);
    24 }
    25 int main()
    26 {
    27 #ifndef ONLINE_JUDGE
    28   freopen("aa.in","r",stdin);
    29 #endif
    30   read(n);
    31   for(int i=1;i<=n;i++)read(a[i]);
    32   mi[n]=a[n];
    33   for(int i=n-1;i>=1;i--)mi[i]=min(mi[i+1],a[i]);
    34   for(int i=1;i<=n;i++)
    35     {
    36       int ans=ef(a[i],i,n)-i-1;
    37       printf("%d ",ans);
    38     }
    39 }
    View Code
  • 相关阅读:
    day 71
    day 70
    day 69 Django 视图层(views)and 模板层(templates)
    Python基础语法 第1节课(标识符、注释、缩进、输入输出、变量、数据类型)
    第019讲:函数 我的地盘听我的--函数变量的作用域(课后测试题及答案 )
    第018讲:函数 灵活即强大(课后测试题及答案 )
    第017讲:函数 python的乐高积木(课后测试题及答案 )
    第012课:列表:一个打了激素的数组3(课后习题及解答)
    第011讲:列表:一个打了激素的数组2 (课后习题及答案)
    第016讲:序列!序列! (课后测试题及答案 )
  • 原文地址:https://www.cnblogs.com/mmmqqdd/p/10762980.html
Copyright © 2011-2022 走看看