题意

给你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. react环境搭建及文件配置

    webpack简介 构建工具(基于Nodejs)node(v16)前端工程化. 环境搭建 创建一个空的package.json npm init webpack核心包(提供了API,插件) npm i ...

  2. 金灿灿的季节 - Apache DolphinScheduler收获5位新Committer

    在这个金灿灿的收获季节,经过 Apache DolphinScheduler PPMC 们的推荐和投票,Apache DolphinScheduler 收获了 5 位新Committer .他们是:n ...

  3. mybatis 07: sql标签中 "#{}" 和 "${}" 的作用和比较

    "#{}"占位符 作用 传参大部分使用"#{}",在数据库底层使用的是:PreparedStatement预编译处理对象 数据库底层被解析为"?&qu ...

  4. Luogu3802 小魔女帕琪 (排列组合)

    注意除数为0情况 #include <iostream> #include <cstdio> #include <cstring> #include <alg ...

  5. ruby 字符注音标签

    <ruby/>标签下的文本可以注音,注音由一对<rt/>标签完成. <ruby> 汉<rt>han</rt> 字<rt>zi&l ...

  6. C++ 运行单个实例,防止程序多次启动

    利用内核对象 封装的类,使用运行单个实例,防止多次启动Demo 例子下载地址:http://pan.baidu.com/share/link?shareid=3202369154&uk=303 ...

  7. Java开发学习(二十九)----Maven依赖传递、可选依赖、排除依赖解析

    现在的项目一般是拆分成一个个独立的模块,当在其他项目中想要使用独立出来的这些模块,只需要在其pom.xml使用<dependency>标签来进行jar包的引入即可. <depende ...

  8. openstack中Keystone组件简解

    一.Keystone服务概述 在Openstack框架中,keystone(Openstack Identity Service)的功能是负责验证身份.校验服务规则和发布服务令牌的,它实现了Opens ...

  9. SpringMVC前置复习以及扩展

    SpringMVC ssm:mybatis+Spring+SpringMVC javaSE javaweb 框架 理解的DAO层和Service层 先简单来讲下Dao层,和Service层的概念: S ...

  10. GIN and RUM 索引性能比较

    gin索引字段entry构造的TREE,在末端posting tree|list 里面存储的是entry对应的行号. 别无其他信息.rum索引,与GIN类似,但是在posting list|tree的 ...