Codeforces 91B-Queue

Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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. Thedispleasure 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.

Examples
input
6
10 8 5 3 50 45
output
2 1 0 -1 0 -1 
input
7
10 4 6 3 2 8 15
output
4 2 1 0 -1 -1 -1 
input
5
10 3 1 10 11
output
1 0 -1 -1 -1 
题意:给一个序列,对于第i个数字a[i],在右边找到一个比它小的数,并且最靠右的位置k,输出k-i-1,如果一个都找不到,输出-1。对于序列的每个元素都要输出。
解题思路:可以用线段树,每个节点表示该段的最大值,每个位置找到最靠右且比它小的数后,将其改为INF,并对线段树进行更新。也可以用单调队列,从最后一个数开始处理,若该数比队列中最后一个都小,则是-1,并加入队尾,否则就对队列中的数进行二分

线段树

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;

#define INF 0x3f3f3f3f

int a[100090],tree[400060];
int n,ans[100090];

void build(int k,int l,int r)
{
    if(l==r)
    {
        scanf("%d",&a[l]);
        tree[k]=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    tree[k]=min(tree[k<<1],tree[k<<1|1]);
}

void update(int k,int l,int r,int p)
{
    if(l==r)
    {
        tree[k]=INF;
        return;
    }
    int mid=(l+r)>>1;
    if(p<=mid) update(k<<1,l,mid,p);
    else update(k<<1|1,mid+1,r,p);
    tree[k]=min(tree[k<<1],tree[k<<1|1]);
}

void query(int k, int l, int r,int p)
{
    if(l==r)
    {
        ans[p]=l-p-1;
        return;
    }
    int mid=(l+r)>>1;
    if(tree[k<<1|1]<a[p]) query(k<<1|1,mid+1,r,p);
    else query(k<<1,l,mid,p);
}

int main()
{
    while(~scanf("%d",&n))
    {
        build(1,1,n);
        for(int i=1; i<=n; i++)
        {
            if(tree[1]>=a[i]) ans[i]=-1;
            else query(1,1,n,i);
            update(1,1,n,i);
        }
        for(int i=1; i<n; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}


单调队列

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;
const int MAXN=1000010;

int a[MAXN],ans[MAXN];
int x[MAXN],p[MAXN];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        int sum=0;
        for(int i=n; i>=1; i--)
        {
            if(sum==0||x[sum-1]>=a[i])
            {
                x[sum]=a[i];
                p[sum++]=i;
                ans[i]=-1;
            }
            else
            {
                int k,l=0,r=sum-1;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(x[mid]<a[i]) {k=mid;r=mid-1;}
                    else l=mid+1;
                }
                ans[i]=p[k]-i-1;
            }
        }
        printf("%d",ans[1]);
        for(int i=2; i<=n; i++)
            printf(" %d",ans[i]);
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值