B. Queue
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
codeforces.com/problemset/problem/91/B
Description
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]);
}