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. 【转载】Emdedding向量技术在蘑菇街推荐场景的应用

    花名:越祈 部门:算法中心搜索策略组 入职时间:2017/06/01 主要从事蘑菇街推荐算法相关研发工作 蘑菇街是一家社会化导购电商平台,推荐一直是其非常重要的流量入口.在电商平台中,推荐的场景覆盖到 ...

  2. MySQL读写分离如何实现?

    主要说下读写分离, 当我们的数据量很大时,数据库服务器的压力变大,这时候我们需要从架构方面来解决这一问题,在一个网站中读的操作很多,写的操作很少,这时候我们需要配置读写分离,把读操作和写操作分离出来, ...

  3. 82.常用的返回QuerySet对象的方法使用详解:all,select_related

    1. all: 返回这个ORM模型的QuerySet对象. articles = Article.objects.all() print(articles) 2.select_related: 查找数 ...

  4. 干货 | 用Serverless快速在APP中构建调研问卷

    Serverless 计算将会成为云时代默认的计算范式,并取代 Serverful (传统云)计算模式,因此也就意味着服务器 -- 客户端模式的终结. ------<简化云端编程:伯克利视角下的 ...

  5. one_day_one_linuxCmd---tar命令

    <坚持每天学习一个 linux 命令,今天我们来学习 tar 命令> 摘要:tar 命令是一个 Linux 下的打包程序,通常在 Linux 下,打包和压缩是不同的程序,打包通过 tar ...

  6. faster rcnn 源码学习-------数据读入及RoIDataLayer相关模块解读

    参考博客:::https://www.cnblogs.com/Dzhen/p/6845852.html 非常全面的解读参考:::https://blog.csdn.net/DaVinciL/artic ...

  7. 使用linux服务器安装wordpress博客详细教程

    前言 最近读了<软技能:代码之外的生存指南>,这本书给了我很大的启示.之前虽然知道作为一个程序员,应该拥有自己的博客,以便于提升自己的知名度,但是并没有了解的过于详细.这本书描写博客的作用 ...

  8. 自定义的listbox支持拖放

    unit unit2; interface uses Classes, Controls, StdCtrls; type TListBox2 = class(TCustomListBox) prote ...

  9. delphpi tcp 服务和客户端 例子

    //服务器端unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, ...

  10. Aras Innovator客户端批量下载关联文件

    <button onclick="btnDownload();" id="downfilebtn">批量下载关联文件</button> ...