Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A
2 seconds
256 megabytes
standard input
standard output
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
5 3
0 4 5 6 7
2
1 0
0
1
5 0
1 2 3 4 5
0
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
思路:先排序,然后求Mex。与x进行比较,如果mex=x,输出0;如果mex>x,直接去掉x,所以输出1;mex<x时,向数列中填数直到mex>x或者mex=0,输出操作的次数。需要注意的是,如果数位不足,mex的最大值依然小于x,需要额外判断。
#include<iostream>
using namespace std; int main()
{
int k;
int re;
int rec;
int mex;
int temp;
int n,x;
int a[];
cin>>n>>x;
for (int i=; i<n; i++) cin>>a[i];
for (int i=; i<n; i++)
{
for (int j=i+; j<n; j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
} mex=a[n-]+;
re=n-;
for(int i=; i<n; i++)
{
if (a[i]!=i)
{
mex=i;
re=i-;
break;
}
} if (mex==x) cout<<;
else if (mex>x) cout<<;
else
{
rec=;
while(mex<x&&mex!=a[n-]+)
{
rec++;
int q=mex+;
re++;
k=re;
re=n-;
mex=a[n-]+;
for(int i=k;i<n;i++)
{
if(a[i]!=q)
{
re=i-;
mex=q;
break;
}
q++;
}
}
if(mex==x) cout<<rec;
else if(mex>x) cout<<rec+;
else cout<<(x-mex)+rec;
} return ;
}
Codeforces 862A Mahmoud and Ehab and the MEX的更多相关文章
- CF 862A Mahmoud and Ehab and the MEX【数组操作】
A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- 【Codeforces Round #435 (Div. 2) A】Mahmoud and Ehab and the MEX
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 让x没有出现,以及0..x-1都出现就可以了. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/std ...
- codeforces E. Mahmoud and Ehab and the function(二分+思维)
题目链接:http://codeforces.com/contest/862/problem/E 题解:水题显然利用前缀和考虑一下然后就是二分b的和与-ans_a最近的数(ans_a表示a的前缀和(奇 ...
- codeforces D. Mahmoud and Ehab and the binary string(二分)
题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...
随机推荐
- jQuery ajax的提交
1.利用jQuery中的aja提交数据,首先引入jQuery中的文件 2.jquery.form.js下载地址:http://vdisk.weibo.com/s/thY_x31gX0M-p?categ ...
- jsp 四大域范围
JSP中四种作用域的不同 作用域规定的是变量的有效期限. 1.如果把变量放到pageContext里,就说明它的作用域是page,它的有效范围只在当前jsp页面里. 从把变量放到pageCont ...
- JavaWeb基础之JdbcUtils工具类1.0
2016年12月20日,第一次学习JDBC.看的是传智播客崔希凡老师的视频,东北口音很是风趣幽默,技术之牛让人膜拜.2017年9月21日,再次重温web知识,分享JdbcUtils工具类,用以接下来的 ...
- 【疑问】用python写登录验证遇到的问题
最近开始断断续续学习python,今天加入博客园,作为新人,和各位老师们讨教了,以后多多照顾!为了大家能看清楚所以就截图了,文末尾附源码,说不定会有那位老师给我指教一番.############### ...
- 根据Dockerfile创建docker dotnet coer 镜像
那我们先来看看Dockerfile文件内容,注意这个文件是没后缀名的. #依赖原始的镜像,因为我们是要创建dotnet coer镜像,所以我就用了官方给的镜像[microsoft/dotnet:lat ...
- 简述static关键字、void与void *(void指针)、函数指针
static关键字1.修饰局部变量,延长局部变量的生命周期.使变量成为静态局部变量,在编译时就为变量分配内存,直到程序退出才释放存储单元.2.修饰全局变量,限制全局变量的使用范围为本文件中.全局变量默 ...
- PE格式第七讲,重定位表
PE格式第七讲,重定位表 作者:IBinary出处:http://www.cnblogs.com/iBinary/版权所有,欢迎保留原文链接进行转载:) 一丶何为重定位(注意,不是重定位表格) 首先, ...
- UEFI启动视频详解:启动分析+N项操作实例
============================================================= ※※※※最给力的视频解说※※※※ 2011hiboy全部共享资料:立刻去 ...
- UWP appButtonBar样式
UWP 的appButtonBar使用<AppBarButton Icon = "Next" Label = "Next" /> Icon是 Sym ...
- OpenWRT UCI命令实现无线中继
本文主要功能主要是利用OpenWRT系统uci命令实现无线中继,主要是利用uci程序修改/etc/congfig/目录下的配置文件.实现步骤如下主要分为以下几步: 1) 安装 relayd (opkg ...