题目链接:https://atcoder.jp/contests/abc205/tasks/abc205_d
题目描述
You are given a sequence of N positive integers: A=(A1,A2,…,), and Q queries.
In the ii-th query (1≤i≤Q), given a positive integer Ki, find the Ki-th smallest integer among the positive integers that differ from all of A1,A2,…,.
数据范围
- 1≤N,Q≤
- 1≤A1<A2<⋯<
≤
- 1≤
≤
- All values in input are integers.
输入
Input is given from Standard Input in the following format:
N Q A1 A2 …… AN K1 K2 ⋮ KQ
输出
Print Q lines. The i-th line should contain the response to the i-th query.
思路分析
大意:要求从1到无限的第个没有出现的数
此题的数据较大,死做太慢,最好用二分法的 lower_bound
#include<iostream>
#include <algorithm>
using namespace std;
long long s[100001];long long s1[100001];long long k[100001];
int main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;++i){
cin>>s[i];
}
s1[0]=0; s[0]=0;
for(int i=1;i<=n;++i){
s1[i]=s[i]-s[i-1]-1+s1[i-1];
}
for(int i=0;i<q;++i){
long long b;
cin>>b;
int h=lower_bound(s1,s1+1+n,b)-s1;
k[i]=b+s[h-1]-s1[h-1];
}
for(int i=0;i<q;++i){
cout <<k[i]<<endl;
}
return 0;
}
仅供参考,请多指教
该博客介绍了如何利用二分查找算法高效解决一类查询问题:在给定一系列递增正整数的情况下,对于每个查询,找出比这些数大的且是第k小的正整数。博主通过C++代码展示了如何应用lower_bound函数实现这一过程,并给出了题目数据范围和输入输出格式。
9210

被折叠的 条评论
为什么被折叠?



