Codeforces 91B-Queue

来源:互联网 发布:java管理系统源代码 编辑:程序博客网 时间:2024/05/22 09:06

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
610 8 5 3 50 45
output
2 1 0 -1 0 -1 
input
710 4 6 3 2 8 15
output
4 2 1 0 -1 -1 -1 
input
510 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 0x3f3f3f3fint 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 longconst 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;}

0 0