签到题。
题目大意
有 种产品,每种包含两颗糖果,选择两种不同产品使得四颗糖果能平分给双胞胎且两人糖果类型完全相同。
主要思路
将每对糖果排序保证 ,用 map 统计每对 出现次数,另用 map 统计相同类型对 出现次数。
相同类型对 内部组合:从 个中选 个。
不同类型对 内部组合:从 个中选 个。
不同类型对 和 之间的组合:用总和与平方和计算所有不同 的组合数。
AC code
#include <bits/stdc++.h>
#define int long long
#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 4e6 + 7;
const int MOD = 998244353;
const int INF = 0X3F3F3F3F;
const int dx[] = {-1, 1, 0, 0, -1, -1, +1, +1};
const int dy[] = {0, 0, -1, 1, -1, +1, -1, +1};
int a[N] , c[N] , b[N] ;
bool v[N] ;
int n , k , m ;
int t ;
map<PII, int> mp;
map<int, int> sm;
inline int read ()
{
int s = 0 , w = 1 ;
char ch = getchar () ;
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar () ;
}
while (ch >= '0' && ch <= '9')
{
s = s * 10 + ch - '0' ;
ch = getchar ();
}
return s * w ;
}
inline void write (int x)
{
if (x < 0) putchar ('-') , x = -x ;
if (x > 9) write (x / 10) ;
putchar (x % 10 + '0') ;
return ;
}
signed main()
{
fst ;
cin >> n;
for(int i = 1; i <= n; i ++)
{
int x, y;
cin >> x >> y;
if(x > y) swap(x, y);
mp[{x, y}] ++;
if(x == y) sm[x] ++;
}
int ans = 0;
int sum = 0, sum2 = 0;
for(auto &p : sm)
{
int c = p.second;
if(c >= 2) ans += c * (c - 1) / 2;
sum += c;
sum2 += c * c;
}
ans += (sum * sum - sum2) / 2;
for(auto &p : mp)
{
int x = p.first.first, y = p.first.second, c = p.second;
if(x == y) continue;
if(c >= 2) ans += c * (c - 1) / 2;
}
cout << ans;
return 0;
}