J - Maximize Diversity of an Array
原题链接:https://vjudge.z180.cn/contest/413777#problem/J
You are given an array A. The diversity of the array A is defined as number of pairs i,j,i<j such that Ai≠Aj.
You want to maximize the diversity of the array. For that, you are allowed to make at most K operations on it, in any of which, you can select a particular element and change its value to any integer in the range 1 to 109, both inclusive.
Find out the maximum diversity of the array that you can obtain.
###Input:
First line will contain T, number of testcases. Then the testcases follow.
The first line of each testcase contains two integers N,K. where N denotes the length of array A.
The next line of each testcase contains N space separated integers, the i-th of which denotes Ai.
###Output:
For each testcase, output in a single line, the answer corresponding to the testcase, which should be an integer denoting the maximum possible diversity.
###Constraints
1≤T≤20
0≤K≤109
2≤N≤105
1≤Ai≤109
###Sample Input:
3
3 10
1 2 3
4 2
1 1 2 2
6 2
2 3 3 2 4 4
###Sample Output:
3
6
14
题目大意:在更改元素个数小于K的情况下,找到序列中能够组成一对 Ai≠Aj 的个数
输入 T:T组数据
输入 N,K: 序列中元素的个数,最大可以更改的个数
输出 :Ai≠Aj 的最大个数
如果序列中没有重复元素 一共有n*(n-1)对
如果有重复元素 一共出现了三次 2 3 4 1 1 1 那么1 1 1 之间有3 对 需要在总数里减去
也就是 先更改出现次数最多的 --优先队列
10发WA…真不戳!我真棒!
WA模板:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
using namespace std;
priority_queue <int> qq;
int main()
{
int t,n,k;
scanf("%d",&t);
while(t--)
{
int x[200055];
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&x[i]);
long long int sum=n*(n-1)/2;
if(k<n)
{
while(!qq.empty()) qq.pop();
sort(x,x+n);
int cnt=1;
for(int i=0;i<n-1;i++)
{
if(x[i]==x[i+1]) cnt++;
else
{
if(cnt>1) qq.push(cnt);
cnt=1;
}
}
if(cnt>1) qq.push(cnt);
while(k--)
{
if(!qq.empty())
{
int tt=qq.top();
qq.pop();
if(tt>2) qq.push(tt-1);
}
else break;
}
while(!qq.empty())
{
int tt=qq.top();
qq.pop();
sum-=(long long)tt*(tt-1)/2;
}
}
printf("%lld\n",sum);
}
return 0;
}