有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离

用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数

对于此题,可用两个队列来维护,一个保存值,一个保持位置,每次查询对值进行二分,然后到对应保持位置的队列中求出结果

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int a[N],ans[N];
vector<int>v,num;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
for(int i=;i<n;i++)cin>>a[i];
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 j=v.size()-1;j>=0;j--)cout<<v[j]<<" ";
cout<<endl;*/
}
for(int i=;i<n;i++)
cout<<ans[i]<<(i==n-?'\n':' ');
return ;
}

CodeForces - 91B单调队列的更多相关文章

  1. Strip CodeForces - 487B (单调队列)

    题面: Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Ale ...

  2. codeforces 939F 单调队列优化dp

    F. Cutlet time limit per test 4 seconds memory limit per test 256 megabytes input standard input out ...

  3. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  4. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  5. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

  6. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  7. Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法

    题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...

  8. Codeforces 940 E.Cashback (单调队列,dp)

    Codeforces 940 E.Cashback 题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序 ...

  9. Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)

    题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...

随机推荐

  1. Debian中安装使用sudo命令

     Debian中安装使用sudo命令  sudo可以让非root用户具有管理员的权限,安装好的Debian后还不能使用sudo,需要使用root用户登陆后安装sudo命令.#apt-get insta ...

  2. python阳历转阴历,阴历转阳历

    #!/usr/bin/env python # coding:utf8 # author:Z time:2019/1/16 import sxtwl # 日历中文索引 ymc = [u"十一 ...

  3. Jenkins系统+独立部署系统

    原文出自:http://os.51cto.com/art/201601/504846.htm 有了Jenkins,为什么还需要一个独立的部署系统? 现在已经有Jenkins,它自身提供了丰富的部署插件 ...

  4. spoj8222

    地址: 题目: NSUBSTR - Substrings no tags  You are given a string S which consists of 250000 lowercase la ...

  5. spark学习(基础篇)--(第三节)Spark几种运行模式

    spark应用执行机制分析 前段时间一直在编写指标代码,一直采用的是--deploy-mode client方式开发测试,因此执行没遇到什么问题,但是放到生产上采用--master yarn-clus ...

  6. selenium的基本介绍

    应吴姑娘(漂亮的姑娘)之邀,加上我师兄(屌丝)和国新(屌丝),组了个四黑小团伙,每周二分享点东西,感觉就是四个辣鸡相互取暖.可惜,今天早上直接是睡过去了,下午都捐给了<白夜追凶>---没办 ...

  7. Autowire

    Field userService in com.demo.web.Controller.HomeController required a single bean, but 2 were found ...

  8. Jquery中val、text、html的区别

    html就是你可以添加像<a></a>.<p></p>等标记text只能写文本如果写了上面的标记则会以文本形式输出val是属性,只有有该属性的对象才能调 ...

  9. 20145201李子璇《网络对抗》逆向及Bof基础实践

    20145201李子璇<网络对抗>逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...

  10. 源码安装GCC-4.9.2

    本文参考:http://cuchadanfan.blog.51cto.com/9940284/1689556  感谢原作者的分享! 首先安装基础包,安装网络依赖的时候要用 [root@localhos ...