首页 > 其他 > 详细

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

时间:2015-03-24 23:10:28      阅读:325      评论:0      收藏:0      [点我收藏+]

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;
}

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

原文:http://blog.csdn.net/guard_mine/article/details/44597451

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