题解:AT_abc418_c [ABC418C] Flush
赛时脑子抽筋了,没前缀和,警示后人
思路
鸽巢原理都听说过吧?这题就是一道鸽巢原理。如果 我们就取 个茶包,否则就全拿,最后再加上任意一袋茶包,一定有 袋相同的茶包。对于不可能的情况,如果 比当前最多的茶包还多,就不可能有 袋相同的茶包。
所以我们先输入数组,然后用前缀和算出前 的茶包的和,最后用二分找出 最后的位置,计算即可。
温馨提示
十年 OI 一场空,不开 long long 见祖宗!
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3*1e5+1;
int n,q;
int a[N],sum[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];//前缀和
}
while(q--){
int x;
cin>>x;
if(x==1){
cout<<"1\n";
continue;
}
if(x>a[n]){
cout<<"-1\n";
continue;
}
int pos=lower_bound(a+1,a+1+n,x)-a-1;//二分
cout<<sum[pos]+(x-1)*(n-pos)+1<<"\n";
}
return 0;
}