计蒜客题目链接:https://nanti.jisuanke.com/t/41387

题目大意:给定一组无序序列,从第一个数开始,求最远比这个数大m的数,与这个数之间相隔多少数字?如果没有输出-1,否则输出间隔了多少数字。

题解:从后往前遍历,在遍历的同时维护一个递增队列,若当前的数大于队尾就进队,否则从该队列中二分找最小的比自己大至少  的数,二者之间的距离即为答案,这里我用vector模拟这个队列。若当前数小于队尾,那这个数一定没有队尾的数优,因为它既比队尾的数靠前,又比它小。

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int main(){
int n;
long long m;
scanf("%d%lld",&n,&m);
int arr[n+1];
for(int i = 0;i<n;i++){
scanf("%lld",&arr[i]);
}
vector<long long int> increase_seq;//存储递增序列
map<long int,long int> index;//存储递增序列元素的索引
vector<long long int > ans;
for(long long int i = n-1;i>=0;i--){
if(i == n-1){
increase_seq.push_back(arr[i]); //遍历的第一个数一定放入队列
index[arr[i]] = i;
ans.push_back(-1);
}
else{
if(increase_seq[increase_seq.size() -1] < arr[i]){
increase_seq.push_back(arr[i]);
index[arr[i]] = i;
ans.push_back(-1);
}
else{
long long int t = arr[i] + m;
int v = lower_bound(increase_seq.begin(),increase_seq.end() ,t ) - increase_seq.begin() ;
if(v == increase_seq.size() ){//在队列中二分
ans.push_back(-1);
}
else{
ans.push_back(index[increase_seq[v]] - i -1);
}
}
}
}
for(int i = ans.size() -1;i>=0;i--){
if(i == ans.size()-1){
printf("%lld",ans[i]);
continue;
}
printf(" %lld",ans[i]);
}
return 0;
}

2019 ICPC徐州网络赛 E. XKC's basketball team(二分)的更多相关文章

  1. 2019 徐州icpc网络赛 E. XKC's basketball team

    题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...

  2. 2019 ICPC 徐州网络赛 B.so easy (并查集)

    计蒜客链接:https://nanti.jisuanke.com/t/41384 题目大意:给定n个数,从1到n排列,其中有q次操作,操作(1) 删除一个数字 // 操作(2)求这个数字之后第一个没有 ...

  3. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  4. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  5. 计蒜客 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 ...

  6. 计蒜客 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 ...

  7. 2018 icpc 徐州网络赛 F Features Track

    这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...

  8. 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)

    题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...

  9. 2019 ICPC 沈阳网络赛 J. Ghh Matin

    Problem Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurr ...

随机推荐

  1. django csrf 中间件

    CSRF和中间件 CSRF使用 说明csrf存在cookie中 全局使用 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', ...

  2. centos因为安装花生壳而无法登录系统的问题

    服务器安装 phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. 3.进入到si ...

  3. BZOJ的思维题

    5085:最大 给你一个n×m的矩形,要你找一个子矩形,价值为左上角左下角右上角右下角这四个数的最小值,要你最大化矩形 的价值. 关键点是要想到把这些值排序 值从小到大考虑,比如说现在最小的值是(x1 ...

  4. 模拟退火SA刷题记录

    洛谷P1337 [JSOI2004]平衡点 / 吊打XXX 基本上是照着别人的代码写的,模拟退火为什么一定能找到答案呢...迷惑,,有时间搜一搜证明啥的 sa步骤:这个是要确定一个(xi,yi)使得函 ...

  5. tk(三)按钮的事件绑定

    (三)按钮的事件绑定 ==1.普通的Button绑定事件== (1)说明: Button 使用 command=功能函数 来绑定 Button(win, text="确定", co ...

  6. SSH、telnet配置以及它们之间区别

    命令: SSH ip domain-name www.baidu.com --配置主机名(用来远程访问) user privilege secret --配置账户名和密码 line vty --配置端 ...

  7. 【C语言】求s(n)=a+aa+aaa+...+aa...a的值

    原理:比如a=2,s(1)=2,s(2)=2+2*10+2,s(3)=2+2*10+2+(2*10+2)*10+2   ..... 规律: item=item*10+a sum=sum+item 代码 ...

  8. 【转载】深入理解Java虚拟机笔记---运行时栈帧结构

    栈帧(Stack Frame)是用于支持虚拟机进行方法调用和方法执行的数据结构,它是虚拟机运行时数据区的虚拟机栈(Virtual Machine Stack)的栈元素.栈帧存储了方法的局部变量表,操作 ...

  9. Lumen跨域问题,PHP CORS Middleware

    前导 本篇文字作为?<「新轮子」PHP CORS (Cross-origin resource sharing),解决 PHP 项目程序设置跨域需求.>的续篇. 简介 什么是 medz/c ...

  10. Docker最全教程——从理论到实战(十二)

    前言 Ubuntu是一个以桌面应用为主的开源GNU/Linux操作系统,应用很广.本篇主要讲述Ubuntu下使用SSH远程登录并安装Docker,并且提供了Docker安装的两种方式,希望对大家有所帮 ...