SDUT 2020 Autumn Team Contest---45(2) //组队赛补题

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值