Codeforces Round #435 (Div. 2) A. Mahmoud and Ehab and the MEX(思路)
2017-09-20 09:58
399 查看
题目:
A. Mahmoud and Ehab and the MEX
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?
Input
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.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
input
output
input
output
input
output
Note
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为不存在于这个集合中的最小非负整数,问需要最少进行几次操作可以使一个集合的MEX为样例给出的数。把集合中出现过的先标记一下,然后遍历输出
代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<iostream>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 100000+20#define ll longlong
using namespace std;
int a[100+20];
int vis[100+20];
int main()
{
int n,x;
mem(vis,0);
scanf("%d%d",&n,&x);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
vis[a[i]]=1;
}
sort(a,a+n);
if(x==0)
{
if(a[0]==0)
puts("1");
else
puts("0");
}
else
{
int sum=0;
for(int i=0; i<x; i++)
{
if(!vis[i])
sum++;
}
if(vis[x])
sum++;
printf("%d\n",sum);
}
return 0;
}
A. Mahmoud and Ehab and the MEX
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?
Input
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.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
input
5 3 0 4 5 6 7
output
2
input
1 0 0
output
1
input
5 0 1 2 3 4 5
output
0
Note
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为不存在于这个集合中的最小非负整数,问需要最少进行几次操作可以使一个集合的MEX为样例给出的数。把集合中出现过的先标记一下,然后遍历输出
代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<iostream>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 100000+20#define ll longlong
using namespace std;
int a[100+20];
int vis[100+20];
int main()
{
int n,x;
mem(vis,0);
scanf("%d%d",&n,&x);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
vis[a[i]]=1;
}
sort(a,a+n);
if(x==0)
{
if(a[0]==0)
puts("1");
else
puts("0");
}
else
{
int sum=0;
for(int i=0; i<x; i++)
{
if(!vis[i])
sum++;
}
if(vis[x])
sum++;
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- Codeforces Round #435 (Div. 2) 之Mahmoud and Ehab and the MEX
- 【Codeforces Round #435 (Div. 2) A】Mahmoud and Ehab and the MEX
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
- CodeForces 862A Mahmoud and Ehab and the MEX
- Codeforces Round #435 (Div. 2): C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function
- Mahmoud and Ehab and the MEX
- 【Codeforces Round #435 (Div. 2) C】Mahmoud and Ehab and the xor
- codeforces 862A Mahmoud and Ehab and the MEX
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function
- Codeforces --- Mahmoud and Ehab and the MEX
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function
- A. Mahmoud and Ehab and the MEX