J - Maximize Diversity of an Array

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

要优先改出现数目多的数目,可以用优先队列来做,注意答案爆int

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N = 1e5 + 10;
unordered_map<int, int> vis;
priority_queue<int> q;
int s[N], num[N], a[N];
int k, n, i, j, r, T, t;
LL ans;

int main() {
    ios::sync_with_stdio(false);
    cin >> T;
    while (T--) {
        memset(s, 0, sizeof(s));
        memset(num, 0, sizeof(num));
        vis.clear();
        cin >> n >> k;
        j = 1;
        ans = (LL)(n - 1) * n / 2;
        for (i = 0; i < n; i++) {
            cin >> a[i];
            if (vis[a[i]] == 0) vis[a[i]] = j++;
            s[vis[a[i]]]++;
        }
        if (k < n) {
            for (i = 1; i < j; i++) {
                if (s[i] > 1) {
                    q.push(s[i]);
                }
            }
            while (k--) {
                if (!q.empty()) {
                    t = q.top();
                    q.pop();
                    if (t > 2) q.push(t - 1);
                }
            }
            while (!q.empty()) {
                t = q.top();
                q.pop();
                ans -= (LL)t * (t - 1) / 2;
            }
        }
        cout << ans << '\n';
    }
}
   There is an empty matrix M of size n×m . Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix M using permutations of length m . That is, each row of M must be a permutation of length m† . Define the value of the i -th column in M as vi=MEX(M1,i,M2,i,…,Mn,i)‡ . Since Daniel likes diversity, the beauty of M is s=MEX(v1,v2,⋯,vm) . You have to help Daniel fill in the matrix M and maximize its beauty. † A permutation of length m is an array consisting of m distinct integers from 0 to m−1 in arbitrary order. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation (1 appears twice in the array), and [0,1,3] is also not a permutation (m−1=2 but there is 3 in the array). ‡ The MEX of an array is the smallest non-negative integer that does not belong to the array. For example, MEX(2,2,1)=0 because 0 does not belong to the array, and MEX(0,3,1,2)=4 because 0 , 1 , 2 and 3 appear in the array, but 4 does not. DeepL 翻译    有一个大小为 n×m 的空矩阵 M 。 中考结束了,丹尼尔想做一些益智游戏。他打算用长度为 m 的排列填充矩阵 M 。也就是说, M 的每一行都必须是长度为 m† 的排列。 将 M 中 i /th列的值定义为 vi=MEX(M1,i,M2,i,…,Mn,i)‡ 。因为丹尼尔喜欢多样性,所以 M 的美是 s=MEX(v1,v2,⋯,vm) 。 你必须帮助丹尼尔填入矩阵 M ,并最大化它的美感。 † 长度为 m 的排列是由 m 个不同的整数组成的数组,这些整数从 0 到 m−1 按任意顺序排列。例如, [1,2,0,4,3] 是一个排列,但 [0,1,1] 不是一个排列( 1 在数组中出现了两次), [0,1,3] 也不是一个排列( m−1=2 ,但数组中有 3 )。 ‡ 数组的 MEX 是不属于数组的最小非负整数。例如, MEX(2,2,1)=0 是因为 0 不属于数组,而 MEX(0,3,1,2)=4 是因为 0 、 1 、 2 和 3 出现在数组中,但 4 不属于数组。    Input The first line of input contains a single integer t (1≤t≤1000 ) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers n and m (1≤n,m≤2⋅105 ) — the size of the matrix. It is guaranteed that the sum of n⋅m over all test cases does not exceed 2⋅105 . DeepL 翻译    输入 输入的第一行包含一个整数 t ( 1≤t≤1000 ) - 测试用例的数量。测试用例说明如下。 每个测试用例的唯一一行包含两个整数 n 和 m ( 1≤n,m≤2⋅105 ) - 矩阵的大小。 保证所有测试用例
03-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值