2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem (状态压缩)
2017-09-24 17:32
405 查看
The frequent subset problem is defined as follows. Suppose UU={1,
2,\ldots…,N}
is the universe, and S_{1}S1, S_{2}S2,\ldots…,S_{M}SMare MM sets
over UU.
Given a positive constant \alphaα, 0<\alpha
\leq 10<α≤1,
a subset BB (B
\neq 0B≠0)
is α-frequent if it is contained in at least \alpha
MαM sets
of S_{1}S1, S_{2}S2,\ldots…,S_{M}SM,
i.e. \left
| \left \{ i:B\subseteq S_{i} \right \} \right | \geq \alpha M∣{i:B⊆Si}∣≥αM.
The frequent subset problem is to find all the subsets that are α-frequent. For example, let U=\{1,
2,3,4,5\}U={1,2,3,4,5}, M=3M=3, \alpha
=0.5α=0.5,
and S_{1}=\{1,
5\}S1={1,5}, S_{2}=\{1,2,5\}S2={1,2,5}, S_{3}=\{1,3,4\}S3={1,3,4}.
Then there are 33 α-frequent
subsets of UU,
which are \{1\}{1},\{5\}{5} and \{1,5\}{1,5}.
The first line contains two numbers NN and \alphaα,
where NN is
a positive integers, and \alphaα is
a floating-point number between 0 and 1. Each of the subsequent lines contains a set which consists of a sequence of positive integers separated by blanks, i.e., line i
+ 1i+1 contains S_{i}Si, 1
\le i \le M1≤i≤M .
Your program should be able to handle NN up
to 2020 and MM up
to 5050.
The number of \alphaα-frequent
subsets.
2017
ACM-ICPC 亚洲区(南宁赛区)网络赛
你怎么看待2017ICPC南宁网络赛,躺着看啊,我已经扶不起来了,彻彻底底输给英语没有!
这个题就是求所有集合中子集出现概率大于等于K的总的子集的个数。
最多20个数,50个集合,关键是不给集合的个数,不给每个集合元素的个数,这题目很可以。要求20个数在50个集合中是否存在,枚举的话需要20!次,肯定超时,顺理成章的想到状态压缩,把每个集合都状态压缩一下,这样就构成了一个新的数,再去枚举每一种状态,也就是每个字符在每一个串中是否出现的情况,把这两种状态&运算,得到的数代表原集合中有多少个这样的子集,最后将得到概率跟K比较,大于等于就自加,注意精度问题,要k值要减去eps才能得到结果。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-12;
const int maxn=400005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int a[101];
int main()
{
int n,x,i;
double k;
cin>>n>>k;
n=(1<<n);
mset(a,0);
int top=1;
while(scanf("%d",&x)!=EOF)
{
a[top]+=(1<<(x-1));
if(getchar()=='\n')
top++;
}
int ans=0;
for(i=1;i<n;i++)
{
int c=0;
for(int j=1;j<=top;j++)
{
if((a[j]&i)==i)
c++;
}
if(1.0*c/top>=k-esp)
ans++;
}
cout<<ans<<endl;
return 0;
}
2,\ldots…,N}
is the universe, and S_{1}S1, S_{2}S2,\ldots…,S_{M}SMare MM sets
over UU.
Given a positive constant \alphaα, 0<\alpha
\leq 10<α≤1,
a subset BB (B
\neq 0B≠0)
is α-frequent if it is contained in at least \alpha
MαM sets
of S_{1}S1, S_{2}S2,\ldots…,S_{M}SM,
i.e. \left
| \left \{ i:B\subseteq S_{i} \right \} \right | \geq \alpha M∣{i:B⊆Si}∣≥αM.
The frequent subset problem is to find all the subsets that are α-frequent. For example, let U=\{1,
2,3,4,5\}U={1,2,3,4,5}, M=3M=3, \alpha
=0.5α=0.5,
and S_{1}=\{1,
5\}S1={1,5}, S_{2}=\{1,2,5\}S2={1,2,5}, S_{3}=\{1,3,4\}S3={1,3,4}.
Then there are 33 α-frequent
subsets of UU,
which are \{1\}{1},\{5\}{5} and \{1,5\}{1,5}.
Input Format
The first line contains two numbers NN and \alphaα,where NN is
a positive integers, and \alphaα is
a floating-point number between 0 and 1. Each of the subsequent lines contains a set which consists of a sequence of positive integers separated by blanks, i.e., line i
+ 1i+1 contains S_{i}Si, 1
\le i \le M1≤i≤M .
Your program should be able to handle NN up
to 2020 and MM up
to 5050.
Output Format
The number of \alphaα-frequentsubsets.
样例输入
15 0.4 1 8 14 4 13 2 3 7 11 6 10 8 4 2 9 3 12 7 15 2 8 3 2 4 5
样例输出
11
题目来源
2017ACM-ICPC 亚洲区(南宁赛区)网络赛
你怎么看待2017ICPC南宁网络赛,躺着看啊,我已经扶不起来了,彻彻底底输给英语没有!
这个题就是求所有集合中子集出现概率大于等于K的总的子集的个数。
最多20个数,50个集合,关键是不给集合的个数,不给每个集合元素的个数,这题目很可以。要求20个数在50个集合中是否存在,枚举的话需要20!次,肯定超时,顺理成章的想到状态压缩,把每个集合都状态压缩一下,这样就构成了一个新的数,再去枚举每一种状态,也就是每个字符在每一个串中是否出现的情况,把这两种状态&运算,得到的数代表原集合中有多少个这样的子集,最后将得到概率跟K比较,大于等于就自加,注意精度问题,要k值要减去eps才能得到结果。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-12;
const int maxn=400005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int a[101];
int main()
{
int n,x,i;
double k;
cin>>n>>k;
n=(1<<n);
mset(a,0);
int top=1;
while(scanf("%d",&x)!=EOF)
{
a[top]+=(1<<(x-1));
if(getchar()=='\n')
top++;
}
int ans=0;
for(i=1;i<n;i++)
{
int c=0;
for(int j=1;j<=top;j++)
{
if((a[j]&i)==i)
c++;
}
if(1.0*c/top>=k-esp)
ans++;
}
cout<<ans<<endl;
return 0;
}
相关文章推荐
- M. Frequent Subsets Problem - 状态压缩-2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem (LIS)
- 周末训练笔记+ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛+2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(9.23,9.24)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L题 非递减权值最大
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H A Cache Simulator
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B.Train Seats Reservation(模拟)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- Minimum Distance in a Star Graph BFS/康托展开 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem
- Finding the Radius for an Inserted Circle--2017 ACM-ICPC 亚洲区(南宁赛区)网络赛