题目链接: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;
}
仅供参考,请多指教