Codeforces Beta Round #75 (Div. 1 Only)---B.Queue

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 test(s)
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,求出一个最远的j,a[j] < a[i]
线段树就行了

/*************************************************************************
    > File Name: CF-75-B.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月24日 星期二 19时53分02秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100100;
int arr[N];
int ans[N];
struct node
{
    int l, r, val;
}tree[N << 2];

void build(int p, int l, int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if (l == r)
    {
        tree[p].val = arr[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    tree[p].val = min(tree[p << 1].val, tree[p << 1 | 1].val);
}

int query(int p, int l, int r, int lim)
{
    if (tree[p].l == tree[p].r)
    {
        return tree[p].val < lim ? tree[p].l : -1;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (l > mid && tree[p << 1 | 1].val < lim)
    {
        int j = query(p << 1 | 1, l, r, lim);
        if (j != -1)
        {
            return j;
        }
    }
    if (r <= mid && tree[p << 1].val < lim)
    {
        int j = query(p << 1, l, r, lim);
        if (j != -1)
        {
            return j;
        }
    }
    if (l <= mid && r > mid && tree[p << 1 | 1].val < lim)
    {
        int j = query(p << 1 | 1, mid + 1, r, lim);
        if (j != -1)
        {
            return j;
        }
    }
    if (l <= mid && r > mid && tree[p << 1].val < lim)
    {
        int j = query(p << 1, l, mid, lim);
        if (j != -1)
        {
            return j;
        }
    }
    return -1;
}

int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &arr[i]);
        }
        build(1, 1, n);
        for (int i = 1; i < n; ++i)
        {
            int j = query(1, i + 1, n, arr[i]);
            if (j == -1)
            {
                ans[i] = -1;
            }
            else
            {
                ans[i] = j - i - 1;
            }
        }
        ans[n] = -1;
        printf("%d", ans[1]);
        for (int i = 2; i <= n; ++i)
        {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值