The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]
\(\mathcal{Description}\)
给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数
\(2\leq n\leq 5*10^5,0\leq m\leq 10^9\)
\(\mathcal{Solution}\)
这道题看了下其他题解都是用线段树写的
虽然线段树是一个很显然的方法,但是代码冗长并且常数较大 (可能是我不喜欢数据结构)
如果把数据范围开大\(3,4\)倍就妥妥的\(T\)了
这里提供一个单调栈的打法
从后往前考虑每个位置
我们把每个答案为\(-1\)的位置的数用一个递增的单调栈维护起来
每次到一个位置就二分这个单调栈,找到第一个比它大至少\(m\)的位置,然后答案就是它们的距离减\(1\)
为什么只有\(-1\)的位置要放到单调栈里面呢
因为有比它大至少\(m\)的位置,所以这个位置永远不会是最优的
\(\mathcal{Code}\)
/*******************************
Author:Morning_Glory
LANG:C++
Created Time:2019年09月16日 星期一 20时46分27秒
*******************************/
#include <cstdio>
#include <fstream>
using namespace std;
const int maxn = 1000006;
int n,m,mx=-1,loc,t;
int h[maxn],ans[maxn],q[maxn];
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",&h[i]);
ans[n]=-1,q[++t]=n;
for (int i=n-1;i>=1;--i){
int g=h[i]+m;
if (h[q[t]]<g){
ans[i]=-1;
if (h[q[t]]<h[i]) q[++t]=i;
}
else{
int l=1,r=t;
while (l<r){
int mid=(l+r)>>1;
if (h[q[mid]]>=g) r=mid;
else l=mid+1;
}
ans[i]=q[l]-i-1;
}
}
for (int i=1;i<=n-1;++i) printf("%d ",ans[i]);
printf("-1\n");
return 0;
}
如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧
The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]的更多相关文章
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)
题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)
这题其实就是瞎搞,稍微想一想改一改就能过. 排序按值的大小排序,之后从后向前更新node节点的loc值,如果后一个节点的loc大于(不会等于)前一个节点的loc,就把前一个节点的loc值设置为后面的l ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019
A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING
题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...
- G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...
- E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...
随机推荐
- 关于如何重写Controller和Service技术攻关文档
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_39784756/articl ...
- [root@offical nginx]# nginx -t nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1 nginx: con
[root@offical nginx]# nginx -tnginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_mo ...
- RabbitMQ教程C#版 - 工作队列
先决条件本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助如果您在阅读本教程时遇到困难,可以 ...
- nginx报警:nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
date: 2019-08-12 16:33:05 author: headsen chen notice :个人原创 告警现象: 解决办法:在http的部分添加hash缓冲值 测试:如下图, ...
- pyqt5界面
用pyqt5做了一个小程序,保留一下这个固定格式: import sys from PyQt5 import uic, QtGui from PyQt5.QtGui import QWindow fr ...
- SNAT场景模拟
我的网络配置跟教程中的这个略有不同: web server:192.168.66.101 nat server:192.168.66.188:192.168.6.172 client:192.168. ...
- VS2019及其他多个版本序列号和安装包
Visual Studio2019序列号Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DFVisual Studio 2019 Pro ...
- 【转载】 tf.split函数的用法
原文地址: https://blog.csdn.net/uestc_c2_403/article/details/73350457 由于tensorflow 版本更新问题 用法略有修改 ----- ...
- Python3基础 内置函数 id
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- java输出一个目录下的子目录
java输出一个目录下的子目录 package com.vfsd.core; import java.io.File; public class ListDir { public static voi ...