题目描述
We have N balls. The i-th ball has an integer Ai written on it.For each k=1,2,…,N, solve the following problem and print the answer.
Find the number of ways to choose two distinct balls (disregarding order) from the N−1 balls other than the k-th ball so that the integers written on them are equal.
Constraints
·3≤N≤2×1e5
·1≤Ai≤N
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
A1 A2 … AN
输出
For each k=1,2,…,N, print a line containing the answer.
样例输入
【样例1】
5
1 1 2 1 2
【样例2】
4
1 2 3 4
【样例3】
5
3 3 3 3 3
【样例4】
8
1 2 1 4 2 1 4 1
样例输出
【样例1】
2
2
3
2
3
【样例2】
0
0
0
0
【样例3】
6
6
6
6
6
【样例4】
5
7
5
7
7
5
7
5
提示
样例1解释
Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.
From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.
Thus, the answer for k=1 is 2.
样例2解释
No two balls have equal numbers written on them.
样例3解释
Any two balls have equal numbers written on them.
思路
相同数字,排列组合,预先处理,O(n),不然会爆
代码
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+5;
ll b[N];
ll c[N];
int main()
{
ll n,sum=0,cnt=0;
scanf("%lld",&n);
ll a[n];
for(ll i=0;i<n;i++)
{
scanf("%lld",&a[i]);
b[a[i]]++;
}
for(ll i=1;i<=n;i++)
{
sum+=(b[i]*(b[i]-1))/2;
}
for(ll i=0;i<n;i++)
{
printf("%lld\n",sum+((b[a[i]]-1)*(b[a[i]]-2))/2-(b[a[i]]*(b[a[i]]-1))/2);
}
return 0;
}