CodeForces

来源:互联网 发布:晒书房软件 编辑:程序博客网 时间:2024/05/29 19:21



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

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-1位置的最远距离r已经求得,现在求i的r,如果i-1的x要小于等于i的x并且i-1的r在i的前面那么推出,i-1的r一定是i的r。反之,则不能利用i-1的r来算i的r。接着,我们可以将它们按照x升序排序,就能一直利用上述的信息。并且在这样排完序的情况下,如果不能满足i-1的r在i的前面,那么说明i的r就是i这个位置。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX_N 100500using namespace std;struct node{    int x,ord,r;}q[MAX_N];bool cmp(node a,node b){    if(a.x!=b.x)        return a.x<b.x;    return a.ord<b.ord;}int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&q[i].x);            q[i].ord=i,q[i].r=i;        }        int b[MAX_N];        sort(q+1,q+1+n,cmp);        b[q[1].ord]=-1;        for(int i=2;i<=n;i++)        {            if(q[i-1].r>q[i].ord)            {                q[i].r=q[i-1].r;            }            b[q[i].ord]=q[i].r-q[i].ord-1;        }        for(int i=1;i<=n;i++)            if(i==n)                printf("%d\n",b[i]);            else                printf("%d ",b[i]);    }    return 0;}

原创粉丝点击