UPC 15090 Banned K"/>
UPC 15090 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×105
·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.
样例输入 Copy
【样例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
样例输出 Copy
【样例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.
AC代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a[300010];
long long b[300010];
long long c=0;
int main()
{int n,max1=-999;scanf("%lld",&n);for(int i=1;i<=n;i++){scanf("%lld",&b[i]);a[b[i]]++;if(max1<b[i])max1=b[i];}for(int i=1;i<=max1;i++)if(a[i]!=0) c+=a[i]*(a[i]-1)/2;for(int i=1;i<=n;i++)printf("%lld\n",c-a[b[i]]*(a[b[i]]-1)/2+(a[b[i]]-1)*(a[b[i]]-2)/2);return 0;
}
更多推荐
UPC 15090 Banned K
发布评论