CodeForces - 91B单调队列
有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离
用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数
对于此题,可用两个队列来维护,一个保存值,一个保持位置,每次查询对值进行二分,然后到对应保持位置的队列中求出结果
#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单调队列的更多相关文章
- Strip CodeForces - 487B (单调队列)
题面: Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Ale ...
- codeforces 939F 单调队列优化dp
F. Cutlet time limit per test 4 seconds memory limit per test 256 megabytes input standard input out ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- 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 ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...
- Codeforces 940 E.Cashback (单调队列,dp)
Codeforces 940 E.Cashback 题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序 ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
随机推荐
- 使用js在网页上显示时间
<html> <script> function getDate(){ var d,s,t; d = new Date(); s = d.getFullYear().toStr ...
- 使用Webdriver执行JS
首先,我们使用如下方式初始化driver: WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptEx ...
- 爬虫——请求库之selenium模块
阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解 ...
- 【JXL】导出Excel
基本设置 // File file = new File("d:\\writetest.xlsx");// WritableWorkbook wwb = Workbook.crea ...
- 《Unity3D》通过对象池模式,管理场景中的元素
池管理类有啥用? 在游戏场景中,我们有时候会需要复用一些游戏物体,比如常见的子弹.子弹碰撞类,某些情况下,怪物也可以使用池管理,UI部分比如:血条.文字等等 这些元素共同的特性是:存在固定生命周期,使 ...
- C#使用window API 控制打印纸张大小(转载)
windows一个特点就是设备无关性,这样就给程序控制打印机提供了很好的方法. 首先引用“泥人张”写的打印API类. using System;using System.Collections;usi ...
- centos 升级nginx到1.10.2
之前装的是1.6.3版本,准备升级到1.10.2版本. 1.下载nginx1.10.2 wget http://nginx.org/download/nginx-1.10.2.tar.gz 2.解压缩 ...
- QML Image Element
QML Image Element The Image element displays an image in a declarative user interface More... Image元 ...
- Python3.x:遍历select下拉框获取value值
Python3.x:遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 s ...
- JavaScript 引用【转】
从一个例子说起: var m ={a:’a’, b:’b’}; var n=m; n.c=’c’; 那么在这个时候 , m.c 也会变成 ’c’! 这个问题在我最开始学习 JS 语言时个人一直处于概念 ...