UPC个人赛第十七场:Banned K

题目描述

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值