http://codeforces.com/problemset/problem/91/B

B. Queue

time limit per test:

2 seconds

memory limit per test:

256 megabytes

input:

standard input

output:

standard output

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.

Examples

input

     

output

   -  - 

input

      

output

    - - - 

input

    

output

  - - - 

题意:

给一个序列,对于第i个数字a[i],在右边找到一个比它小的数,并且最靠右的位置k,输出k-i-1,如果一个都找不到,输出-1。对于序列的每个元素都要输出。

解题思路:

可以用线段树,每个节点表示该段的最小值, 循环一遍数组,每个位置找到最靠右且比它小的数后(已经处理过),将其改为INF,并对线段树进行最小值更新。

查询之前, 先询问整个数组的最小值和当前值的比较,满足最小值小于当前值才有查询的必要,如果满足查询的条件,右边不成立就一定在左边。

上代码:

 #include <bits/stdc++.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const double eps =1e-;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; struct node
{
int l;
int r;
int val;
}Tr[maxn<<]; int a[maxn]; //存放数据
int ans[maxn]; //存放答案 void PushUp(int u)
{
Tr[u].val=min(Tr[u<<].val,Tr[u<<|].val);
} void Build(int l,int r,int u)
{
Tr[u].l=l; Tr[u].r=r; Tr[u].val=;
if(l==r)
{
Tr[u].val=a[l];
return ;
}
int mid=(l+r)>>;
Build(l,mid,u<<);
Build(mid+,r,u<<|);
PushUp(u);
} void Update(int u,int pos)
{
int l=Tr[u].l; int r=Tr[u].r;
if(l==r)
{
Tr[u].val=INF; //pos位置的答案已得出,将其变为INF
return ;
}
int mid=(l+r)>>;
if(pos<=mid)
Update(u<<,pos);
else
Update(u<<|,pos);
PushUp(u); //向上更新最小值
} void Query(int u,int pos)
{
int l=Tr[u].l; int r=Tr[u].r;
if(l==r)
{
ans[pos]=l-pos-; //得出pos位置的答案
return ;
}
int mid=(l+r)>>;
if(Tr[u<<|].val<a[pos]) //因为是最右边的最小值,先判断右边
Query(u<<|,pos);
else
Query(u<<,pos);
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
Build(,n,);
for(int i=;i<=n;i++) //遍历求每一个答案
{
if(Tr[].val>=a[i]) ans[i]=-; //不满足查询条件
else Query(,i);
Update(,i); //更新最小值
}
for(int i=;i<=n;i++)
printf(i==n? "%d\n":"%d ",ans[i]); return ;
}

这题也可以用单调队列,从最后一个数开始处理,若该数比队列中最后一个都小,则是-1,并加入队尾,否则就对队列中的数进行二分(直接粘的题解,学完单调队列再回来填坑)

 #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 long
const int INF=0x3f3f3f3f;
const int MAXN=; int a[MAXN],ans[MAXN];
int x[MAXN],p[MAXN]; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++) scanf("%d",&a[i]);
int sum=;
for(int i=n; i>=; i--)
{
if(sum==||x[sum-]>=a[i])
{
x[sum]=a[i];
p[sum++]=i;
ans[i]=-;
}
else
{
int k,l=,r=sum-;
while(l<=r)
{
int mid=(l+r)>>;
if(x[mid]<a[i]) {k=mid;r=mid-;}
else l=mid+;
}
ans[i]=p[k]-i-;
}
}
printf("%d",ans[]);
for(int i=; i<=n; i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}

以下题解来自于:https://www.cnblogs.com/sineatos/p/3870790.html

分析题目,我们发现题目需要我们求的值的要求有两个:①最右边的,②比当前考察的值小的。
  于是我们需要维护一个这样的序列,这个序列保存着已扫描的值里面的最小值,同时这个序列具有单调性,从开始到当前,序列需要保持递减,对于插入的时候如果无法保持序列的单调性的话,就不要需要插入的值,否则就插入。在求结果的时候,我们可以二分求出比考察点小的,里考察点最远(最右)的那个点,然后求出答案即可。

  这里记录一下维护序列单调性的两种方法:①要插入的元素一定会插入,通过抛弃原有的元素来保持单调性。②如果带插入的元素加入会打破单调性的话就不会插入到序列里面。
  如果用一个单调队列的维护一个序列,单调队列里面的元素是已经扫描过的元素的最值,次值,次次值······,同时,如果里面是拥有窗口的话,单调队列里面记录的元素可以是元素保存位置的下标,这样就可以更好地维护元素弹出。

  对于单调性的研究还需继续。

 #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define MAX 100002
using namespace std; int a[MAX],ans[MAX];
vector<int> v,num; int main()
{
int n;
//freopen("data.txt","r",stdin);
while(~scanf("%d",&n)){
for(int i=;i<n;i++) scanf("%d",&a[i]);
v.clear();
num.clear();
for(int i=n-;i>=;i--){
if(v.size()== || v.back()>=a[i]){
v.push_back(a[i]); num.push_back(i);
ans[i]=-;
}else{
int j = (lower_bound(v.rbegin(),v.rend(),a[i]) - v.rbegin());
j = (int)v.size() - j - ;
ans[i] = num[j+] - i - ;
}
}
for(int i=;i<n;i++){
if(i) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return ;
} 91B

CodeForces 91B Queue (线段树,区间最值)的更多相关文章

  1. 【bzoj4695】最假女选手 线段树区间最值操作

    题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...

  2. 【bzoj4355】Play with sequence 线段树区间最值操作

    题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...

  3. 【hdu5306】Gorgeous Sequence 线段树区间最值操作

    题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...

  4. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  5. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  6. HDU-1754I Hate It 线段树区间最值

    这道题比较基本,就是用线段树维护区间最值,可以算是模板吧-.. I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768 ...

  7. BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值

    这道题相对简单下面是题目: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Submit: 6542 Solve ...

  8. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  9. HDU 4819 Mosaic (二维线段树&区间最值)题解

    思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...

随机推荐

  1. Python 操作csv和excel表格

    1. 操作csv表格 使用的库 csv 1. csv文件里读取数据 代码 1. 以列表形式打开 import csv f = open('csv_test.csv', 'r') # 打开csv文件 c ...

  2. C语言I作业博客07

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9935 我在这个课程的目 ...

  3. org.springframework.dao.CannotAcquireLockException异常分析

    错误信息如下: 2017-09-27 16:27:16.153 - [com.ldyun.base.service.impl.BaseRetailOrderServiceImpl] - 新增零售商品订 ...

  4. 1-4 无监督学习(Unsupervised Learning)

    无监督学习定义: [无监督学习]中没有任何的标签或者是有相同的标签或者就是没标签.所以我们已知数据集,却不知如何处理,也未告知每个数据点是什么.别的都不知道,就是一个数据集.你能从数据中找到某种结构吗 ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习:Number 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. __init__.py在Python中的用处

    1.Python中的package定义很简单,其层次结构与程序所在目录的层次结构相同, 这一点与Java类似,唯一不同的地方在于,python中的package必须包含一个__init__.py的文件 ...

  7. How to .gitignore all files/folder in a folder, but not the folder itself?

    https://stackoverflow.com/questions/4250063/how-to-gitignore-all-files-folder-in-a-folder-but-not-th ...

  8. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  9. zip和rar文件的contentType

    zip.rar.msi等文件使用Chrome提交的ContentType都为null,IE可以正确获取

  10. cf1172E Nauuo and ODT(LCT)

    首先可以转化问题,变为对每种颜色分别考虑不含该颜色的简单路径条数.然后把不是当前颜色的点视为白色,是当前颜色的点视为黑色,显然路径数量是每个白色连通块大小的平方和,然后题目变为:黑白两色的树,单点翻转 ...