Atcoder ABC D - Kth Excluded

题目链接:https://atcoder.jp/contests/abc205/tasks/abc205_d

题目描述

You are given a sequence of N positive integers: A=(A1,A2,…,_{An }), 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,…,_{An}.

数据范围

  • 1≤N,Q≤10^{5}
  • 1≤A1<A2<⋯<_{An}10^{18}
  • 1≤_{Ki}10^{18}
  • 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到无限的第_{Ki}个没有出现的数

此题的数据较大,死做太慢,最好用二分法的 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;
}

仅供参考,请多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值