-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmasking.cpp
More file actions
44 lines (39 loc) · 1.05 KB
/
Bitmasking.cpp
File metadata and controls
44 lines (39 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*Consider this problem: There are N=5000 workers.
Each worker is available during some days of this month (which has 30 days).
For each worker, you are given a set of numbers, each from interval [1,30],
representing his/her availability. You need to assign an important project
to two workers but they will be able to work on the project only when they
are both available. Find two workers that are best for the job — maximize the
number of days when both these workers are available.*/
// bitmasking
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector <int> masks(n,0);
for(int i=0;i<n;++i){
int days;
cin>>days;
for(int j=0;j<days;++j){
int x;
cin>>x;
masks[i]=(masks[i]|(1<<x));
}
}
int maxDays=-1;
int person1=-1;
int person2=-1;
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
int intersection=masks[i]&masks[j];
int commonDays=__builtin_popcount(intersection);
if(commonDays>maxDays){
maxDays=commonDays;
person1=i;
person2=j;
}
}
}
cout<<person1<<" "<<person2<<" "<<maxDays;
}