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.
构造一个数组,使0到x-1都出现,x不能出现,所以找到这个值就好啦
#include<bits/stdc++.h> using namespace std; typedef long long LL; int main() { int n,x; cin>>n>>x; int a[105]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); int pos=lower_bound(a,a+n,x)-a; if(a[pos]==x)pos--; pos=x-pos; cout<<pos<<endl; return 0; }
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.
二分图拥有的最大边数,就是左边点的个数*右边点的个数,还能增加的再减去当前的,我想的直接分组,这样是不对的,因为我会贪心选择他是左边还是右边,这个明显就是错的,所以dfs一下就好的
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e5+5; vector<int>a[N]; int num[2],vis[N]; void dfs(int x,int f){ for(auto X:a[x]){ if(vis[X])continue; num[(f+1)%2]++; vis[X]=1; dfs(X,f+1); } } int main() { int n; cin>>n; int s1=0,s2=0; for(int i=1;i<n;i++) { int u,v; cin>>u>>v; a[u].push_back(v); a[v].push_back(u); } vis[1]=1; num[1]=1; dfs(1,1); cout<<1LL*num[0]*num[1]-n+1<<endl; return 0; }
Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.
Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.
The only line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.
If there is no such set, print "NO" (without quotes).
Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.
5 5
YES
1 2 4 5 7
3 6
YES
1 2 5
You can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR
For the first sample .
For the second sample .
异或嘛,不同得1
但这是一个构造啊,我想不出来
但是要找No 这个数据还是简单的,大概都能找到就是2个数怎么都凑不出0,因为2个相同的数异或为0
所以主要处理的的是n>2得,2^17次方超过1e5了,就利用这个特性构造另两个值就行
#include <bits/stdc++.h> using namespace std; int main() { int n,x; cin>>n>>x; if(n==1)cout<<"YES "<<x; else if(n==2) { if(!x)cout<<"NO"; else cout<<"YES "<<0<<" "<<x; } else { cout<<"YES "; int y=0; for(int i=1;i<n-2;i++) y^=i,cout<<i<<" "; int a=1<<17,b=1<<18; cout<<a<<" "<<b<<" "<<(a^b^x^y); } return 0; }