计蒜客题目链接: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. linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦(三)

    linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦(三) 安装PHP 1.yum方式安装PHP方法同安装apache一样传送门:linux cent ...

  2. [转]从实例谈OOP、工厂模式和重构

    有了翅膀才能飞,欠缺灵活的代码就象冻坏了翅膀的鸟儿.不能飞翔,就少了几许灵动的气韵.我们需要给代码带去温暖的阳光,让僵冷的翅膀重新飞起来.结合实例,通过应用OOP.设计模式和重构,你会看到代码是怎样一 ...

  3. harbor仓库部署时启用https时的常见错误KeyError: 'certificate'等

    出现 KeyError: 'certificate' 错误 先确认你的配置是否正确,例如harbor.yml里的https证书位置是否正确,证书是否正常无误 如果上述无误确反复报错,请确认你的harb ...

  4. File FileStream StreamReader和StreamWriter

    File 静态类 ReadAllBytes 和 WriteAllBytes ,用于一次性全部读取和写入小文件的字节码,                  ReadLine  ReadkAll  用于一 ...

  5. 2018ICPC南京站Problem G. Pyramid

    题意: 找有多少个等边三角形 解析: 首先打标找规律,然后对式子求差分 0,1,5,15,35,70,126,210... 1,4,10,20,35,56... 3,6,10,15,21... 3,4 ...

  6. nginx 简单理解和配置

    1.概念 Nginx是一个高性能的HTTP和反向代理的web服务器,同时也提供了IMAP/POP3/SMTP服务,Nginx是由伊戈尔·塞索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的,第一 ...

  7. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

  8. 解决pjax重复加载js导致事件重复绑定的问题

    个人博客 地址:http://www.wenhaofan.com/article/20180925232057 1.所有js统一在pjax容器外引入 在pjax容器外引入的js只会被引入一次,所以不会 ...

  9. vue报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

  10. RN开发-IDE和API

    一.开发工具 1.Visual Studio Code:微软IDE,轻量级,只有30+M大小 2.nuclide :仅支持Mac 3.WebStorm : JavaScript开发工具(IDE) 二. ...