codeforces1462E2. Close Tuples (hard version)

该博客主要讨论了一种算法问题,其中涉及到对一组数进行排序,并从排序后的数组中选择m个元素,使得这m个元素中包含给定数的最小值和最大值。博主通过预处理数组并计算组合数来解决这个问题,最终得出满足条件的元组数量。

https://codeforces.com/contest/1462/problem/E2#

注意到m元组中坐标是各不相同的,且只要求最大值和最小值,所以我们可以把a排序,反正m元组是无视坐标顺序的

然后我们就可以钦定这个m元组必选当前a[i],现在要求>=a[i]-k中选出剩下m-1个,预处理一下搞个组合数就行了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;

int n,m,k,cnt,tot,cas;ll ans;
int a[maxl];
ll fac[maxl],inv[maxl];
bool vis[maxl];
char s[maxl];

inline ll c(int n,int r)
{
	if(r>n || r<0) return 0;
	assert(r==2);
	return 1ll*n*(n-1)/2;
}

inline void prework()
{
	//scanf("%d%d%d",&n,&m,&k);
	scanf("%d",&n);m=3;k=2;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
}

inline void mainwork()
{
	ans=0;
	for(int i=m;i<=n;i++)
	{
		int l=lower_bound(a+1,a+1+n,a[i]-k)-a;
		(ans+=c(i-l,m-1));
	}
}

inline void print()
{
	printf("%lld\n",ans);
}

int main()
{
	int t=1;
	scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值