AtCoder-Beginner Contest 159 D - Banned K

Problem Statement

We have NN balls. The ii-th ball has an integer A_iAi​ written on it.
For each k=1, 2, ..., Nk=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-1N−1 balls other than the kk-th ball so that the integers written on them are equal.

Constraints

  • 3 \leq N \leq 2 \times 10^53≤N≤2×105
  • 1 \leq A_i \leq N1≤Ai​≤N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A_1A1​ A_2A2​ ...... A_NAN​

Output

For each k=1,2,...,Nk=1,2,...,N, print a line containing the answer.\

Sample Input 1 Copy

5
1 1 2 1 2

Sample Output 1 Copy

2
2
3
2
3

Consider the case k=1k=1 for example. The numbers written on the remaining balls are 1,2,1,21,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=1k=1 is 22.

题目大意:分别取走数组中的第1-N个数,任选两个数,求取出来的两个数相等的组合有多少个。

排列组合的题目:C _{n}^{m}

先把所有的组合全部加起来求出sum,然后输出的时候如果这个数字出现了超过两次就先减去原来这个数字的所有组合,再加上去掉当前数字的所有组合:

#include <cstdio>
#include <iostream>
#define inf 0x3f3f3f3f

using namespace std ;
const int N = 2e5 + 8 ;
using ll = long long ;
ll num[N], mp[N] ;

int main() {
	int n ;
	cin >> n ;
	ll ma = -inf ;
	for (int i = 0 ; i < n ; i ++) {
		cin >> num[i] ;
		mp[num[i]] ++ ;
		ma = max(ma, num[i]) ;
	}
	ll sum = 0 ;

	for (int i = 0 ; i <= ma ; i ++ ) {
		if (mp[i] >= 2 )  // 其实这句话可有可无,观察可以发现当mp[i]等于1或者是0的时候
			sum += mp[i] * (mp[i] - 1 ) / 2 ; //后面加的这个东西的值都等于0 
	}

	for (int i = 0 ; i < n ; i ++) {
		if (mp[num[i]] >= 2)
			cout << sum - mp[num[i]] *( mp[num[i]] - 1 ) / 2 + ( mp[num[i] ] - 2  )*( mp[num[i] ] - 1 ) / 2 << '\n' ;
		else
			cout << sum << '\n' ;
	}
	return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值