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 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...
随机推荐
- P1013
问题 D: P1013 时间限制: 1 Sec 内存限制: 128 MB提交: 33 解决: 21[提交][状态][讨论版] 题目描述 " 找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手 ...
- 使用wsimport和JAX-WS调用Web Service接口
本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...
- SpringMVC框架(四)文件的上传下载,上下文路径
文件目录: SpringMVC配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- 翻译 | 关键CSS和Webpack: 减少阻塞渲染的CSS的自动化解决方案
原文地址: Critical CSS and Webpack: Automatically Minimize Render-Blocking CSS 原文作者: Anthony Gore 译者: 蜗牛 ...
- 简单Elixir游戏服设计-玩法simple_poker
上回介绍了玩法,现在编写了玩法的简单建模. 做到现在感觉目前还没有使用umbrella的必要(也许以后会发现必要吧),model 应用完全可以合并到game_server. 代码还在https://g ...
- C#取得站点跟目录
string strServer = "http://" + Request.ServerVariables["SERVER_NAME"].ToString() ...
- js-location应用
1 location.search ?xxx=sss&yyy=ddd 获取地址中查询的值 /** * 解析url参数 * @example ?id=123456&a=b * @retu ...
- asp.net(C#)利用QRCode生成二维码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs&q ...
- git的使用(入门篇)
1.Git 的安装 Window 下的安装 从 http://git-scm.com/download 上下载window版的客户端,然后一直下一步下一步安装git即可,请注意,如果你不熟悉每个选项的 ...
- 用FastDFS一步步搭建文件管理系统
一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...