B. Queue

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

codeforces.com/problemset/problem/91/B

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

6
10 8 5 3 50 45

Sample Output

2 1 0 -1 0 -1

HINT

题意

给你一个数列,让你找到最右边比这个数小的数的位置,如果没有就输出-1

题解:

直接线段树中二分,查询最小值,然后二分区间就好了~

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} //************************************************************************************** int tr[maxn];
int a[maxn];
int ans[maxn];
int tmp;
void build(int x,int l,int r)
{
if(l==r)
{
tr[x]=a[l];
return;
}
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
tr[x]=min(tr[x<<],tr[x<<|]);
}
void query(int x,int l,int r,int t)
{
if(l==r)
{
ans[tmp++]=l-t-;
return;
}
int mid=(l+r)>>;
if(tr[x<<|]<a[t])
query(x<<|,mid+,r,t);
else
query(x<<,l,mid,t);
}
void update(int x,int l,int r,int t)
{
if(l==r)
{
tr[x]=inf;
return;
}
int mid=(l+r)>>;
if(t<=mid)
update(x<<,l,mid,t);
else
update(x<<|,mid+,r,t);
tr[x]=min(tr[x<<],tr[x<<|]);
}
int main()
{
int n=read();
for(int i=;i<=n;i++)
a[i]=read();
build(,,n);
for(int i=;i<=n;i++)
{
if(tr[]>=a[i])
ans[tmp++]=-;
else
query(,,n,i);
update(,,n,i);
}
for(int i=;i<n;i++)
printf("%d ",ans[i]);
}

Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分的更多相关文章

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

    B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...

  2. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  3. Codeforces Beta Round #67 (Div. 2)

    Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. 修改 AndroidManifest minSdkVersion 的方法

    1.修改AndroidManifest文件    使用16进制编辑器检索位置:FF FF FF FF 08 00 00 10 ??    将??替换为原APK的minSdkVersion对应的16进制 ...

  2. 用DzzOffice管理阿里云OSS

    在DzzOffice分两种方式管理阿里云OSS 1.把阿里云oss作为多人或企业的共享网盘使用. 2.接入个人的阿里云oss管理,可同时管理多个bucket,多个bucket之间可以互传文件. 下面先 ...

  3. BF-KMP 算法

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  4. jQuery对select标签的常用操作

    1.获取当前选中项的value. $("#selector").val(); 2.获取当前选中项的text. $("#selector").find(" ...

  5. java int和String类型之间的相互转换

    String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...

  6. Chapter11:关联容器

    当用decltype来获得一个函数指针类型时,必须加上一个*来指出我们要使用一个给定函数类型的指针.decltype<CompareIsbn>*. map<string, int&g ...

  7. Hadoop2的简单安装

    前面花了很多时间来介绍hadoop1的安装,随着hadoop的发展,hadoop2的应用也越来越普及,hadoop2解决了hadoop1中的很多问题,比如单点故障,namenode容量小的问题. 我们 ...

  8. 使用Log.isLoggable方法

    在Audio Debug过程中想打开AudioService.java文件中的log,比如想打开setmode这段log: if (DEBUG_MODE) { Log.v(TAG, "set ...

  9. Windows Azure Platform 系列文章目录

    Windows Azure Platform (一) 云计算的出现 Windows Azure Platform (二) 云计算的分类和服务层次 Windows Azure Platform (三) ...

  10. C++ 之关联容器 map

    标准库定义了四种关联容器:map是其中之一(另外还有set.multimap.multiset).map的元素以键-值(key-value),在学了顺序容器之后,再学习关联容器,就比较比较好理解了. ...