Codeforces Gym101063 C.Sleep Buddies (2016 USP-ICMC)
It is nighttime in the Earth Colony on Mars and everyone is getting ready to sleep. It is common to sleep in pairs, so that if anything were to happen during the night people could aid each other.
To decide who is a suitable candidate to sleep with whom, the leaders of GEMA asked everyone to answer a questionnaire about attributes they desire their partner to have from a list of M possible items.
To choose the pairs, GEMA uses the Jaccard index between the desired attributes of both persons. The Jaccard index for two sets A and B is defined as , that is, the size of the intersection between A and B divided by the size of their union. They allow a pair to be formed if the Jaccard index of the two attribute sets for the pair is at least K.
Thanks to GEMA, there are too many people living on Mars these days. Help the high commanders decide how many allowed pairs there are out of the N people living there.
Input
The input begins with two integers, N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 10), the number of people on Mars and the number of possible attributes on the questionnaire.
Then follow N lines, each beginning with an integer Q (1 ≤ Q ≤ M), the number of desired attributes on the list of the i-th person. Then follow Q integers q (1 ≤ q ≤ M), encoding these attributes. There numbers are all different.
The last line of input contains a real number K (0 ≤ K ≤ 1), the minimum required Jaccard index for a pair.
Output
Output the number of pairs that are allowed.
Example
2 5
2 1 3
5 3 1 5 4 2
0.66489
0
3 1
1 1
1 1
1 1
0.85809
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const int M = 1e5 + 50; 5 int main() 6 { 7 int t,k=0,n,x,a[M],m,p[20]; 8 ll b[M]; 9 double dd; 10 scanf("%d%d",&t,&m); 11 for(int i=1;i<=m+1;i++){ 12 p[i] = pow(2,i-1); 13 } 14 memset(b,0,sizeof(b)); 15 while(t--){ 16 scanf("%d",&n); 17 memset(a,0,sizeof(a)); 18 for(int i = 0;i < n;i++){ 19 scanf("%d",&x); 20 a[x] = 1; 21 } 22 int ans = 0; 23 for(int i=1;i<=m;i++){ 24 ans += (p[i]*a[i]); 25 } 26 //cout<<ans<<endl; 27 b[ans]++; 28 } //设一个十进制的数,b数组是因为总共不超过1024,然后他有1e5的集合,有重复; 29 cin>>dd; 30 int c[15]; 31 for(int k=1;k<=m;k++) 32 c[k]=pow(2,k-1); 33 ll sum = 0; 34 for(int i = 1;i <= p[m+1]; i++){ 35 for(int j = i;j <= p[m+1]; j++){ 36 int num1 = (i|j),num2=(i&j); 37 double cnt1=0,cnt2=0; 38 for(int k=1;k<=m;k++) 39 { 40 if(num1&c[k]) 41 cnt1++; 42 if(num2&c[k]) 43 cnt2++; 44 } 45 //cout<<cnt1/cnt2<<endl; 46 if(cnt2/cnt1>=dd&&i!=j&&b[i]) 47 sum+=b[i]*b[j]; 48 else if(cnt1/cnt2>=dd&&i==j&&b[i]){ 49 sum+=b[i]*(b[i]-1)/2; 50 } 51 } 52 } 53 cout<<sum<<endl; 54 }