Codeforces Round #690 (Div. 3) E1. Close Tuples (easy version)组合数公式

这是一个关于Codeforces编程竞赛第690轮(Div. 3)中E1题的简单版解析。题目要求找出长度为n的序列a中,元素最大值与最小值之差不超过2的三元组数量。对于给定的序列,通过排序和计算相同数字的计数,可以计算出符合条件的元组数。在示例中,对于序列[1, 2, 4, 3],存在两个这样的元组。输出答案时不考虑模运算。" 123822466,12600475,Linux磁盘管理与文件系统详解,"['Linux', '运维', '文件系统']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON’T NEED to output the answer by modulo.

You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).

Find the number of tuples of m=3 elements such that the maximum number in the tuple differs from the minimum by no more than k=2. Formally, you need to find the number of triples of indices i<j<z such that

max(ai,aj,az)−min(ai,aj,az)≤2.
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1,j=2,z=4 and i=2,j=3,z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.

Input
The first line contains a single integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the sequence a.

The next line contains n integers a1,a2,…,an (1≤ai≤n) — the sequence a.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don’t need to output the answer by modulo. You must output the exact value of the answer.

Example
inputCopy
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
outputCopy
2
4
0
15
只会做简单版本的。。先把数组进行排序并记录相同数字的个数,在排序后遍历1到n,每次找到最小的那个数字在比它大1和大2的数字总数中任取2个进行求和。
这样是过不了hard的,因为在k逼近最大值是,这是一个接近n2的复杂度,得想办法剪一剪QAQ

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
ll a[maxn];
int maxx(int a,int b,int c){
	return max(a,max(b,c));
}
int minn(int a,int b,int c){
	return min(a,min(b,c));
}
ll c(ll n,ll m){
	ll ans=1;
	for(ll i=1;i<=m;i++){
		ans=ans*(n-m+i)/i;
	}
	return ans;
}
void solve(){
	ll t;
    cin>>t;
    while(t--)
    {
		ll n;
		cin>>n;
		for(ll i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+1+n);
		map<ll,ll>mp;
		for(ll i=1;i<=n;i++){
			mp[a[i]]++;
		}
		ll sum=0;
		for(ll i=1;i<=n;i++){
			sum+=c((mp[a[i]]+mp[a[i]+1]+mp[a[i]+2]-1),2);
			mp[a[i]]--;
		}
		cout<<sum<<endl;
    }
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值