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

    题解:

    直接线段树中二分,查询最小值,然后二分区间就好了~

    代码

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 2000001
    #define mod 1000000007
    #define eps 1e-9
    int Num;
    char CH[20];
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    //**************************************************************************************
    
    int tr[maxn];
    int a[maxn];
    int ans[maxn];
    int tmp;
    void build(int x,int l,int r)
    {
        if(l==r)
        {
            tr[x]=a[l];
            return;
        }
        int mid=(l+r)>>1;
        build(x<<1,l,mid);
        build(x<<1|1,mid+1,r);
        tr[x]=min(tr[x<<1],tr[x<<1|1]);
    }
    void query(int x,int l,int r,int t)
    {
        if(l==r)
        {
            ans[tmp++]=l-t-1;
            return;
        }
        int mid=(l+r)>>1;
        if(tr[x<<1|1]<a[t])
            query(x<<1|1,mid+1,r,t);
        else
            query(x<<1,l,mid,t);
    }
    void update(int x,int l,int r,int t)
    {
        if(l==r)
        {
            tr[x]=inf;
            return;
        }
        int mid=(l+r)>>1;
        if(t<=mid)
            update(x<<1,l,mid,t);
        else
            update(x<<1|1,mid+1,r,t);
        tr[x]=min(tr[x<<1],tr[x<<1|1]);
    }
    int main()
    {
        int n=read();
        for(int i=1;i<=n;i++)
            a[i]=read();
        build(1,1,n);
        for(int i=1;i<=n;i++)
        {
            if(tr[1]>=a[i])
                ans[tmp++]=-1;
            else
                query(1,1,n,i);
            update(1,1,n,i);
        }
        for(int i=0;i<n;i++)
            printf("%d ",ans[i]);
    }
  • 相关阅读:
    『在线工具』 基于 xsser.me 源码 + BootStrap 前端 的 XSS 平台
    『Python』Python 调用 ZoomEye API 批量获取目标网站IP
    『Python』 多线程 共享变量的实现
    『Python』 多线程 端口扫描器
    打印机PCL漏洞原理分析
    『GitHub』Git常用命令记录
    『Python』爬行搜索引擎结果获得指定主机二级域名及IP信息
    iOS使用Runtime给分类动态绑定属性
    Swift介绍(基于Swift4版本)
    iOS中的block模板
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4604355.html
Copyright © 2011-2022 走看看