Codeforces 91B-Queue
来源:互联网 发布:java管理系统源代码 编辑:程序博客网 时间:2024/05/22 09:06
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. Thedispleasure 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.
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.
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.
610 8 5 3 50 45
2 1 0 -1 0 -1
710 4 6 3 2 8 15
4 2 1 0 -1 -1 -1
510 3 1 10 11
1 0 -1 -1 -1
题意:给一个序列,对于第i个数字a[i],在右边找到一个比它小的数,并且最靠右的位置k,输出k-i-1,如果一个都找不到,输出-1。对于序列的每个元素都要输出。
解题思路:可以用线段树,每个节点表示该段的最大值,每个位置找到最靠右且比它小的数后,将其改为INF,并对线段树进行更新。也可以用单调队列,从最后一个数开始处理,若该数比队列中最后一个都小,则是-1,并加入队尾,否则就对队列中的数进行二分
线段树
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <queue>#include <stack>using namespace std;#define INF 0x3f3f3f3fint a[100090],tree[400060];int n,ans[100090];void build(int k,int l,int r){ if(l==r) { scanf("%d",&a[l]); tree[k]=a[l]; return ; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k]=min(tree[k<<1],tree[k<<1|1]);}void update(int k,int l,int r,int p){ if(l==r) { tree[k]=INF; return; } int mid=(l+r)>>1; if(p<=mid) update(k<<1,l,mid,p); else update(k<<1|1,mid+1,r,p); tree[k]=min(tree[k<<1],tree[k<<1|1]);}void query(int k, int l, int r,int p){ if(l==r) { ans[p]=l-p-1; return; } int mid=(l+r)>>1; if(tree[k<<1|1]<a[p]) query(k<<1|1,mid+1,r,p); else query(k<<1,l,mid,p);}int main(){ while(~scanf("%d",&n)) { build(1,1,n); for(int i=1; i<=n; i++) { if(tree[1]>=a[i]) ans[i]=-1; else query(1,1,n,i); update(1,1,n,i); } for(int i=1; i<n; i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } return 0;}
单调队列
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAXN=1000010;int a[MAXN],ans[MAXN];int x[MAXN],p[MAXN];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) scanf("%d",&a[i]); int sum=0; for(int i=n; i>=1; i--) { if(sum==0||x[sum-1]>=a[i]) { x[sum]=a[i]; p[sum++]=i; ans[i]=-1; } else { int k,l=0,r=sum-1; while(l<=r) { int mid=(l+r)>>1; if(x[mid]<a[i]) {k=mid;r=mid-1;} else l=mid+1; } ans[i]=p[k]-i-1; } } printf("%d",ans[1]); for(int i=2; i<=n; i++) printf(" %d",ans[i]); printf("\n"); } return 0;}
- Codeforces 91B-Queue
- codeforces 91b Queue
- 解题报告 之 CodeForces 91B Queue
- CodeForces 91B-K - Queue 二分
- 单调队列 CodeForces 91B Queue
- CodeForces 91B Queue【线段树】
- CodeForces 490B Queue
- 【CODEFORCES】 B. Queue
- Codeforces 490B - Queue (模拟)
- Codeforces 490B Queue(水)
- [CodeForces 490B]Queue[实现]
- Codeforces 767 B The Queue
- CodeForces 767B The Queue
- codeforces 91B Queue 线段树,简单查询
- CodeForces 91B Queue (线段树单点操作)
- Codeforces Round #279 (Div. 2)B. Queue
- Codeforces Round #279 (Div. 2) B. Queue
- CodeForces-266B Queue at the School
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- 算法库algorithm-0-概览
- 错误解决 Can't create handler inside thread that has not called Looper.prepare()
- Cesium viewer 最常用的控件 API
- extjs 循环执行多个异步请求时,参数和后台以及相关问题和衍生问题的处理
- Codeforces 91B-Queue
- Let’s Hook a Library Function_linux
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- HTTP版本和连接时长的解析
- 这是我的第一个博客
- 如何快速使用freemarker
- Android高效加载大图、多图和LruCache缓存
- it-linux-yum-安装epel源
- 因SGA分配错误无法启动数据库