UPC 15090 Banned K

编程入门 行业动态 更新时间:2024-10-07 09:26:58

<a href=https://www.elefans.com/category/jswz/34/1753214.html style=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

本文发布于:2024-02-07 05:50:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UPC   Banned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!