码迷,mamicode.com
首页 > 其他好文 > 详细

状态压缩泛做

时间:2021-02-05 10:29:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:arc   子集   枚举   math   复杂   状态   its   strong   cin   

P3052 [USACO12MAR]Cows in a Skyscraper G

预处理出每种状态的体积和,然后枚举子集转移。

时间复杂度 \(O\left(3^n\right)\)

code:

#include<bits/stdc++.h>
using namespace std;
#define N 262144
#define Min(x,y)((x)<(y)?x:y)
#define For(i,x,y)for(i=x;i<=(y);i++)
int s[N],f[N],w[19];
int main()
{
	int n,i,j;
	cin>>n>>w[0];
	For(i,1,n)cin>>w[i];
	For(i,1,(1<<n)-1)
	{
		For(j,1,n)
		if(i&1<<(j-1))s[i]+=w[j];
		f[i]=n;
	}
	For(i,1,(1<<n)-1)
	for(j=i;j;j=(j-1)&i)
	if(s[j]<=w[0])f[i]=Min(f[i],f[i^j]+1);
	cout<<f[(1<<n)-1];
	return 0;
}

但是要开 O2 才能过,考虑复杂度更低的做法。

状态压缩泛做

标签:arc   子集   枚举   math   复杂   状态   its   strong   cin   

原文地址:https://www.cnblogs.com/May-2nd/p/14371664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!