题意

给你n个非负整数的数列a,你可以进行K次操作,每次操作可以将任意位置的数数更改成任意一个非负整数,求操作以后,DIFF(a)-MEX(a)的最小值;DIFF代表数组中数的种类。MEX代表数组中未出现的最小自然数。

提示

1. 显然 DIFF(a)-MEX(a)最小,DIFF(a)越小越好,MEX(a)越大越好

2. 假如 DIFF 降低,同时 MEX 提升,这样操作是不亏的,因此我们只需要提升MEX即可,贪心的的构造0-x,x为k次修改,能构建到mex的最大的数列a状态。

3. 在原始a中,0-x中空缺的值即为需要填充个数的值,我们只需要贪心,先填入出现次数少的>x的值,以降低它的DIFF,即MEX固定了,再降低其DIFF

代码

#include<bits/stdc++.h>

using namespace std;
int a[100005], cnt[100005];
map<int, int> mp;
struct node {
int x, y;
} op[100005]; void run() {
int n, k;
cin >> n >> k;
for (int i = 0; i <= n; i++) {
cnt[i] = 0;
}
set<int> s;
mp.clear();
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] < n)cnt[a[i]]++;
s.insert(a[i]);
mp[a[i]]++;
} int res = 0, flag = n;
for (int i = 0; i < n; i++) {
if (cnt[i] == 0)res++;
if (res > k) {
flag = i;
break;
}
}
int st = 0;
for (auto [x, y]: mp) {
if (x >= flag)op[++st] = {x, y};
}
sort(op + 1, op + 1 + st, [&](const node &x, const node &y) { return x.y < y.y; });
int sum = 0;
int ree = min(res, k);
for (int i = 1; i <= st; i++) {
ree -= op[i].y;
if (ree >= 0)sum++;
else break;
}
cout << min(res, k) - sum + int(s.size()) - flag << '\n';
} int main() {
int t;
cin >> t;
while (t--)
run();
return 0;
}

Codeforces 1684 E. MEX vs DIFF的更多相关文章

  1. codeforces#1139E. Maximize Mex(逆处理,二分匹配)

    题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...

  2. Codeforces 1083C Max Mex [线段树]

    洛谷 Codeforces 思路 很容易发现答案满足单调性,可以二分答案. 接下来询问就转换成判断前缀点集是否能组成一条链. 我最初的想法:找到点集的直径,判断直径是否覆盖了所有点,需要用到树套树,复 ...

  3. Codeforces 1083C Max Mex

    Description 一棵\(N\)个节点的树, 每个节点上都有 互不相同的 \([0, ~N-1]\) 的数. 定义一条路径上的数的集合为 \(S\), 求一条路径使得 \(Mex(S)\) 最大 ...

  4. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  5. 【Codeforces 1083C】Max Mex(线段树 & LCA)

    Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...

  6. Codeforces Round #792 (Div. 1 + Div. 2) // C ~ E

    比赛链接:Dashboard - Codeforces Round #792 (Div. 1 + Div. 2) - Codeforces C. Column Swapping 题意: 给定一个n*m ...

  7. Codeforces 740C. Alyona and mex 思路模拟

    C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  8. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

  9. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

随机推荐

  1. SP104 Highways (矩阵树,高斯消元)

    矩阵树定理裸题 //#include <iostream> #include <cstdio> #include <cstring> #include <al ...

  2. Luogu5367 【模板】康托展开 (康拓展开)

    \(n^2\)暴力 #include <iostream> #include <cstdio> #include <cstring> #include <al ...

  3. fast json 乱序问题解决过程

    解决问题:保存到redis中的jsonstring在转回jsonObject的时候乱序: 解决方案:https://inlhx.iteye.com/blog/2312512 解决过程: 1 看fast ...

  4. spring使用junit单元测试

    <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test& ...

  5. 【HMS core】【FAQ】典型问题合集7

    ​1.[HMS core][Account Kit][问题描述] 集成华为帐号服务后,登录服务异常,无法获取用户信息,报statusCode为907135001,抓取报错日志:Failed to re ...

  6. [CF1537E] Erase and Extend (字符串)

    题面 给一个长度为 n \tt n n 的字符串,你可以进行无限次以下两种操作之一: 删去末尾的字符(此时要保证删去后字符串非空). 把当前整个字符串复制一份,接到自己的后面. 输出最终通过操作能达到 ...

  7. KingbaseES DENSE_RANK 函数用法

    DENSE_RANK()函数用于为结果集分区内的每一行分配一个排名,排名值之间没有差距,函数为结果集的每个分区中的每一行分配一个等级. 与 RANK() 函数不同的是,DENSE_RANK() 函数总 ...

  8. Ros入门21讲

    一.ROS是什么? ROS=通信机制+开发工具+应用功能+生态系统 目的:提高机器人研发中的软件复用率. 1.ROS中的通信机制 松耦合分布式通信: 注意:什么是耦合.紧耦合.松耦合? 1.1 耦合 ...

  9. 洛谷P7112 行列式求值

    行列式求值 这是一个让你掉头发的模板题 行列式的定义 行列式 (\(\texttt{Determinant}\)) 是一个函数定义,取值是一个标量. 对一个 \(n\times n\) 的矩阵 \(A ...

  10. docker学习笔记-容器相关命令

    新建并启动容器 docker pull centos (先下载镜像,如果没有直接使用docker run 命令会根据本地情况进行下载) # docker run [可选参数] image # 参数说明 ...