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

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

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

#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. MongoDB主从复制+集群

    一.读写分离的概念 读写分离,基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库处理SELECT查询操作.数据库复制被用来把事务性操作导致的变更同步 ...

  2. 在MFC下面实际演示CCriticalSection 的使用

    Q:CCriticalSection是什么? A:CCriticalSection是一种线程同步策略 或者说技术 或者方法  总之呢就是这么个意思.... 参考资料: http://blog.csdn ...

  3. idea新建的项目,文件夹右键不能新建class

    一般情况下,新建的mave项目,通常没有XXX\src\main\java这个目录,如果手动创建,则又不能右键build与java相关的,强行建立的话,也不会被idea所识别,更不会被虚拟机编译执行. ...

  4. 在xshell中使用sftp上传文件

    Xshell 5 (Build 1335)Copyright (c) 2002-2017 NetSarang Computer, Inc. All rights reserved. Type `hel ...

  5. [转]eclipse 配置黑色主题 Luna 方式三

      虽然以前也使用eclipse的黑色主题,但是配置起来稍微麻烦一点. 这里先声明,下面的方式适合最新版本的Eclipse Luna,旧的版本可以下载我提供的这个插件,并将其放在eclipse目录下的 ...

  6. mysql 手动安装和管理

    版本:5.7.10 my.ini简单配置 [client] default-character-set=utf8 [mysqld] port = 3306 basedir =D:/programs/M ...

  7. .NET BETWEEN方法

    Between 值范围比较 可以判断一个值是否落在区间范围值中. public static bool Between<T>(this T me, T lower, T upper) wh ...

  8. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  9. 【Thinking in Java, 4e】控制流程执行

    p46~p75: [迭代] 1.Java不允许将数字作为布尔值用. 1.有点意思的小程序WhileTest. public class WhileTest { static boolean condi ...

  10. Swoole学习(六)Swoole之定时器的创建与清除

    环境:Centos6.4,PHP环境:PHP7,Swoole2.1(在指定的时间后执行函数,需要1.7.7或更高版本) <?php //----------------------------- ...