D. Yarik and Musical Notes

一道有趣的思维题
经过推理,最后的答案只有两种构成:
1.1的数目*2的数目
2.所有相同的数n,进行C(2,n)然后相加
像这样,然后把比值设成k,可以知道只能枚举如上两类

https://codeforces.com/problemset/problem/1899/D

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;




queue<ll>id;
int main()
{
	int t; cin >> t;
	for (int ii = 0; ii < t; ii++)
	{
		map<ll, ll>mplst;
		ll ans = 0;
		int n; cin >> n; int xx;
		if (n == 1) { cout << 0 << endl; cin >> xx; }
		else
		{
			for (int i = 0; i < n; i++)
			{
				cin >> xx;
				if (!mplst[xx])id.push(xx);
				mplst[xx]++;

			}
			ans += mplst[1] * mplst[2];//第一类
			while (!id.empty())
			{
				ll idx = id.front();
				id.pop();
				if (mplst[idx] > 1)ans += mplst[idx] * (mplst[idx] - 1) / 2;//第二类
			}
			cout << ans << endl;
		}
	}
	return 0;
}

posted on 2024-03-22 22:00  WHUStar  阅读(8)  评论(0编辑  收藏  举报