【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate
题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055
思路:
设d[S][r][c]表示形如r*c的矩形是否可以划分为S中的子集,10表示可否。
转移方程:
d[S][r][c] = d[S0][r0][c] || d[S0][r][c0]
优化:
首先注意到S r c三者知二求一,所以将状态优化为d[S][x]表示有短边x的矩形是否可以分为S的子集,长边==sum(S)/x ,这样恰好迎合了另外一个优化——只计算r*c==S的状态。
代码:
#include<iostream>
#include<cstring>
using namespace std; const int maxn = +;
const int maxw = + ; int d[<<maxn][maxw],vis[<<maxn][maxw];
int sum[<<maxn];
int kase=; inline int bitcount(int x) { return x==?:bitcount(x/)+(x&); } int dp(int s,int x) {
if(vis[s][x]==kase) return d[s][x]; //记忆化搜索
vis[s][x]=kase;
if(bitcount(s)==) return d[s][x]=; //搜索边界 int& ans=d[s][x];
int y=sum[s]/x; //根据s与x计算y
for(int s0=(s-)&s;s0;s0=(s0-)&s) { //枚举子集 //一刀
int s1=s-s0; //划分成两个子集
if(sum[s0]%x== && dp(s0,min(x,sum[s0]/x)) && dp(s1,min(x,sum[s1]/x)) ) return ans=; //是纵向一刀
if(sum[s0]%y== && dp(s0,min(y,sum[s0]/y)) && dp(s1,min(y,sum[s1]/y)) ) return ans=; //抑或横向一刀
//如果有一种切法 分成的两个子矩形YES的话那么该矩阵为YES
} return ans=;
} int main() {
ios::sync_with_stdio(false);
int n,x,y;
int A[maxn];
memset(vis,,sizeof(vis)); while(cin>>n && n) {
cin>>x>>y;
for(int i=;i<n;i++) cin>>A[i]; int full=(<<n)-;
for(int s=;s<=full;s++){ //离线计算集合s之和
sum[s]=;
for(int j=;j<n;j++) if(s&(<<j)) sum[s] += A[j];
} int ans;
if(sum[full]!=x*y || sum[full]%x!=) ans=; //面积相等且形如x*y
else
ans=dp(full,min(x,y)); cout<<"Case "<<++kase<<": ";
cout<<(ans? "YES" : "No")<<"\n";
}
return ;
}
【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate的更多相关文章
- UVALive 4794 Sharing Chocolate
Sharing Chocolate Chocolate in its many forms is enjoyed by millions of people around the world ever ...
- UVALive 4794 Sharing Chocolate(状压,枚举子集)
n的规模可以状压,f[x][y][S]表示x行,y列,S集合的巧克力能否被切割. 预处理出每个状态S对应的面积和sum(S),对于一个合法的状态一定满足x*y=sum(S),实际上只有两个变量是独立的 ...
- UVALive 4794 Sharing Chocolate DP
这道题目的DP思想挺先进的,用状态DP来表示各个子巧克力块.原本是要 dp(S,x,y),S代表状态,x,y为边长,由于y可以用面积/x表示出来,就压缩到了只有两个变量,在转移过程也是很巧妙,枚举S的 ...
- LA 4794 Sharing Chocolate
大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...
- LA 4794 - Sharing Chocolate dp
题意 有一块\(x*y\)的巧克力,问能否恰好分成n块,每块个数如下 输入格式 n x y a1 a2 a3 ... an 首先\(x \times y 必然要等于 \sum\limits_{i=1} ...
- UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【暑假】[深入动态规划]UVAlive 3983 Robotruck
UVAlive 3983 Robotruck 题目: Robotruck Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format ...
- 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp
A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...
- UVa 1009 Sharing Chocolate (数位dp)
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- js添加创建节点和合并节点
var _div = document.createElement("div"), //创建节点 txt1 = document.createTextNode("123& ...
- Maven的Dependency怎么找?
用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称 ...
- MFC的dll中控制资源问题
有程序EXE和DLL,其中DLL中有1个函数用来显示对话框,被EXE调用.当EXE和DLL都为Release或Debug时,没有任何问题,但EXE为Release.DLL为Debug时,就会出错.该D ...
- ADT(android-bundler) HTML EDIT 编辑 xml HTML
逗比的ADT,安装个html一直不成功,最后发现了如下方法 Helper_Install New Software_ http://download.eclipse.org/releases/indi ...
- redis 性能监控和排查
最近项目中接连遇到redis出现瓶颈的问题,现在把排查的一些经验记录下来备查,本篇只是思路的整理,不涉及具体的使用. 大体的思路如下: 1.通过slow log查看 参考 http://www.cnb ...
- PHP的执行原理/执行流程
http://www.cnblogs.com/hongfei/archive/2012/06/12/2547119.html 更深入的学习和了解可以查看下面: 风雨的博客http://www.laru ...
- 安装tomcat出现failed to install tomcat8 service错误及解决方法
failed to install tomcat8 service 如下图所示: 一.安装tomcat出现failed to install tomcat6 service错误及解决方法(转载 ...
- SQL Server中的分页
sqlserver2000时的分页思路 .分页查询时,首先将数据排序 select * from MyStudent order by fid desc .取第一页数据 * from MyStuden ...
- ConfigurationManager配置操作
/// <summary> /// 配置信息维护 /// </summary> public class AppConfig { public static Configura ...
- NuGet相关的文章
NuGet学习笔记(1)——初识NuGet及快速安装使用http://www.cnblogs.com/zhwl/p/3377510.html NuGet学习笔记(2) 使用图形化界面打包自己的类库ht ...