C - Sleep Buddies Gym - 101063C
题目链接:
https://vjudge.net/problem/473487/origin
Description:
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
3
题意:
N个人,M种属性,每个人有Qi个性属性,分pair要求两个人的
>=K,问有多少对人满足。
Hint:
M只有10,考虑状压,二进制位保存没有这种属性。
AC代码:
#include<bits/stdc++.h> #define LL long long #define maxn 100010 #define eps 1e-6 using namespace std; int n,m,q,Q; LL vis[maxn]; LL num[maxn]; double k; set<int> s; vector<int> G[20]; int solve(int x){ int res=0; while(x>0){ if(x&1) res++; x>>=1; } return res; } int main(){ cin>>n>>m; int now=0,Q; for(int i=1;i<=n;i++){ scanf("%d",&Q); now=0; for(int j=1;j<=Q;j++){ scanf("%d",&q); now+=(1<<(q-1)); } s.insert(now); vis[now]++; } cin>>k; LL ans=0; LL aa,bb; for(int i=0;i<(1<<10);i++){ if(vis[i]==0) continue; for(int j=i+1;j<(1<<10);j++){ if(vis[j]==0) continue; now=(i&j); aa=solve(now); bb=solve(i)+solve(j)-solve(now); if((double)aa/bb>=k){ ans+=(vis[i]*vis[j]); } } aa=vis[i]; if(aa>=2){ ans+=(aa*(aa-1))/2; } } cout<<ans<<endl; return 0; }