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的更多相关文章

  1. UVALive 4794 Sharing Chocolate

    Sharing Chocolate Chocolate in its many forms is enjoyed by millions of people around the world ever ...

  2. UVALive 4794 Sharing Chocolate(状压,枚举子集)

    n的规模可以状压,f[x][y][S]表示x行,y列,S集合的巧克力能否被切割. 预处理出每个状态S对应的面积和sum(S),对于一个合法的状态一定满足x*y=sum(S),实际上只有两个变量是独立的 ...

  3. UVALive 4794 Sharing Chocolate DP

    这道题目的DP思想挺先进的,用状态DP来表示各个子巧克力块.原本是要 dp(S,x,y),S代表状态,x,y为边长,由于y可以用面积/x表示出来,就压缩到了只有两个变量,在转移过程也是很巧妙,枚举S的 ...

  4. LA 4794 Sharing Chocolate

    大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...

  5. LA 4794 - Sharing Chocolate dp

    题意 有一块\(x*y\)的巧克力,问能否恰好分成n块,每块个数如下 输入格式 n x y a1 a2 a3 ... an 首先\(x \times y 必然要等于 \sum\limits_{i=1} ...

  6. 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 ...

  7. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

  8. 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp

    A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...

  9. UVa 1009 Sharing Chocolate (数位dp)

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

随机推荐

  1. maven 解决 Eclipse is running in a JRE, but a JDK is

    解决安装了maven插件的myeclipse每次开启报错 The Maven Integration requires that Eclipse be running in a JDK, becaus ...

  2. VB逆向

    大家或许有所察觉了,随着我们课程的不断深入学习,我们感觉自身逆向的“内功”也在不断的增进! 我们从爆破入手,到现在逐步大家进入程序的内部,认识不同编译器开发的程序,探索不同的加密逻辑. 前边,我们的例 ...

  3. Kafka 之 async producer (1)

    问题 很多条消息是怎么打包在一起的? 如果消息是发给很多不同的topic的, async producer如何在按batch发送的同时区分topic的 它是如何用key来做partition的? 是如 ...

  4. 盘点 OSX 上最佳的 DevOps 工具

    [编者按]对于运维人员来说,他们往往需要各种各样的工具来应对工作需求,近日 Dustin Collins 通过「The Best DevOps Tools on OSX」一文对 OSX 平台上的工具进 ...

  5. VARCHAR2转换为CLOB碰到ORA-22858错误

    近日工作中发现有一张表的字段类型建错了,本应是BLOB类型却被别人建成了VARCHAR2(200),修改时oracle却提示“ORA-22858 invalid alteration of datat ...

  6. MySQL exist

    http://www.cnblogs.com/glory-jzx/archive/2012/07/19/2599215.html http://www.w3school.com.cn/sql/func ...

  7. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  8. 阿里云PHP Redis代码示例

    测试代码示例 <?php /* 这里替换为连接的实例host和port */ $host = "localhost"; $port = 6379; /* 这里替换为实例id和 ...

  9. Tomcat Java内存溢出 PermGen space 解决方案

    -Xms300m -Xmx400m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256M

  10. valgrind基本使用

    1.valgrind是一个内存检测工具,类似的还有purify,insure++等 2.测试文件test.c test.c : main(){ int* a=new int[100]; return ...