您的位置:首页 > 产品设计 > UI/UE

CodeForces 91B Queue【线段树】

2016-10-20 20:41 946 查看
QueueTime Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d& %I64uSubmit Status Practice CodeForces91BDescriptionThere 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 theend 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. Thedispleasure of the i-thwalrus 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 strongerthe displeasure is.The airport manager asked you to count for each of n walruses in the queue his displeasure.InputThe first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. Thesecond 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.OutputPrint n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-thwalrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.Sample InputInput
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
     线段树维护区间最小值, 循环一遍,每次找到比这个数小且在最右端的数,将左边处理过的数可以更新成INF并更新最小值。那么查询之前, 先询问整个数组的最小是和当前值的比较,当前值大于最新最小值才有查询的必要,而且右边不成立就一定在左边。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<climits>#include<string>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>using namespace std;#define rep(i,j,k)for(i=j;i<k;i++)#define per(i,j,k)for(i=j;i>k;i--)#define MS(x,y)memset(x,y,sizeof(x))#define max(a,b) a>b?a:b#define min(a,b) a<b?a:b#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;const int INF=0x7ffffff;#define lson rt<<1, l, m#define rson rt<<1|1, m+1, rconst int maxn = 100005;const int MAX = 0x3f3f3f3f;int n,i,j,k,b;int cnt;int a[maxn],mi[maxn<<2],ans[maxn];void up(int rt){mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);}void build(int rt ,int l,int r){if(l==r){scanf("%d",&mi[rt]);a[l]=mi[rt];return;}int m=(l+r)>>1;build(lson);build(rson);up(rt);}void query(int rt,int l,int r){if(l==r){ans[k++]=l-b-1;return;}int m=(l+r)>>1;if(mi[rt<<1|1]<cnt)query(rson);else query(lson);}void update(int rt,int l,int r){if(l==r)mi[rt]=MAX;else {int m=(l+r)>>1;if(b<=m)update(lson);else update(rson);up(rt);}}int main(){scanf("%d",&n);build(1,1,n);for(int i=1;i<=n;i++){cnt=a[i],b=i;update(1,1,n);if(mi[1]>=cnt)ans[k++]=-1;else query(1,1,n);}for(i=0;i<k;i++)printf("%d ",ans[i]);return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: