跳到正文

题解

题解:AT_abc418_c [ABC418C] Flush

MobiusInversion约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

题解:AT_abc418_c [ABC418C] Flush

At_coder

赛时脑子抽筋了,没前缀和,警示后人

思路

鸽巢原理都听说过吧?这题就是一道鸽巢原理。如果 Ai≥xA_i \ge x 我们就取 x−1x-1 个茶包,否则就全拿,最后再加上任意一袋茶包,一定有 xx 袋相同的茶包。对于不可能的情况,如果 xx 比当前最多的茶包还多,就不可能有 xx 袋相同的茶包。

所以我们先输入数组,然后用前缀和算出前 xx 的茶包的和,最后用二分找出 Ai≥xA_i \ge x 最后的位置,计算即可。

温馨提示

十年 OI 一场空,不开 long long 见祖宗!

C++
#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;
}

记录

原文