跳到正文

题解

题解:CF2051D Counting Pairs

JimmyQ约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年12月26日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟
获赞
8
题目
CF2051D

版本

  1. v1当前

CF2051D Counting Pairs

思路

首先给数组排个序,然后再暴力枚举 ii,不难发现所有符合的 jj 一定是连续的,所以再二分计算出符合数列的开头以及结尾。

AC 代码

C++
#include<bits/stdc++.h>
using namespace std;
#define N 300005
long long t,n,x,y,a[N],ans,sum;
int main(){
	cin>>t;
	while(t--){
		cin>>n>>x>>y;
		sum=ans=0;
		for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
		sort(a+1,a+n+1);
		for(long long i=1;i<=n;i++){
			long long pos1=lower_bound(a+i+1,a+n+1,sum-y-a[i])-a;//开头
			long long pos2=upper_bound(a+i+1,a+n+1,sum-x-a[i])-a-1;//结尾
			if(sum-y-a[i]<=a[pos1]&&a[pos2]<=sum-x-a[i]) ans+=pos2-pos1+1;//别忘了+1
		}
		cout<<ans<<endl;
	}
	return 0;
} 

AC 记录

我才不会告诉你我赛事因为调试代码和数组大小挂了2次。

原文
存档评论3 条抓取于