首页 > 其他 > 详细

Queue

时间:2017-05-25 21:10:27      阅读:416      评论:0      收藏:0      [点我收藏+]

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

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 







#include <algorithm>
#include <stdio.h>
using namespace std;
#define N 100100
int num[N];
int main()
{
    long long m,n,t,i,sum=0;
      scanf("%lld %lld",&n,&m);
      for(i=0;i<n;i++)
        scanf("%d",&num[i]);
    for(i=0;i<n-2;i++)
    {
      t=upper_bound(num,num+n,num[i]+m)-num;
      sum+=(t-i-1)*(t-i-2)/2;//排列组合化简
    }
    printf("%lld",sum);
    return 0;

}

 

 

 

 

 

技术分享

 

Queue

原文:http://www.cnblogs.com/ljhacm/p/6905823.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!