UVALive 4794 Sharing Chocolate DP
这道题目的DP思想挺先进的,用状态DP来表示各个子巧克力块。原本是要 dp(S,x,y),S代表状态,x,y为边长,由于y可以用面积/x表示出来,就压缩到了只有两个变量,在转移过程也是很巧妙,枚举S的子集s0,然后 s1=S-s0来代表除该子集的另一个集合,接下来分两种情况,如果这个子集是通过把 S保留x,切割y,则转移到dp(s0,x)和dp(s1,x),另一种情况是转移到dp(s0,y)和dp(s1,y)。为了更加缩小状态,统一把转移方程的 x换成 min(x,sum[S]/x),sum为该状态下的面积
#include <cstdio>
#include <cstring>
#include <algorithm>
//#include <cmath>
#define N 1<<17
using namespace std;
int f[N][],sum[N];
int A[],vis[N][];
int ok(int x) //测试当前状态下是否只剩下一种巧克力,是的话,二进制状态里必定只有一个1,因此只会返回1 否则则返回其他值.
{
if (x==) return ;
return ok(x/)+(x&);//这里导致我WA了好几次,原因是没注意&的优先级低,要括号起来
}
int dp(int S,int x)
{
if (vis[S][x]) return f[S][x];
vis[S][x]=;
//int& ans=f[S][x];
if (ok(S)==) return f[S][x]=; //如果满足,则说明以及到达某块具体巧克力的状态,完成任务 return 1回去
int y=sum[S]/x;
for (int s0=S&(S-);s0;s0=S&(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 f[S][x]=;//这里分两种情况,分别代表两个子集的产生 是切割y 或者 切割x产生的
if (sum[s0]%y== && dp(s0,min(y,sum[s0]/y))&& dp(s1,min(y,sum[s1]/y)))
return f[S][x]=;
}
return f[S][x]=; }
int main()
{
int kase=,n,x,y;
while (scanf("%d",&n)!=EOF)
{
if (n==) break;
scanf("%d%d",&x,&y);
for (int i=;i<n;i++)
scanf("%d",&A[i]);
memset(sum,,sizeof sum);
for (int i=;i<(<<n);i++)
{
for (int j=;j<n;j++)
{
if (i&(<<j))
{
sum[i]+=A[j];
}
}
}
memset(vis,,sizeof vis);
int all=(<<n)-;
int ans=;
if (sum[all]!=x*y || sum[all]%x!=)
{
ans=;
}
else
{
ans=dp(all,min(x,y));
}
printf("Case %d: %s\n",++kase,ans==? "Yes":"No");
}
return ;
}
UVALive 4794 Sharing Chocolate DP的更多相关文章
- 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...
- UVALive 4794 Sharing Chocolate
Sharing Chocolate Chocolate in its many forms is enjoyed by millions of people around the world ever ...
- LA 4794 - Sharing Chocolate dp
题意 有一块\(x*y\)的巧克力,问能否恰好分成n块,每块个数如下 输入格式 n x y a1 a2 a3 ... an 首先\(x \times y 必然要等于 \sum\limits_{i=1} ...
- UVALive 4794 Sharing Chocolate(状压,枚举子集)
n的规模可以状压,f[x][y][S]表示x行,y列,S集合的巧克力能否被切割. 预处理出每个状态S对应的面积和sum(S),对于一个合法的状态一定满足x*y=sum(S),实际上只有两个变量是独立的 ...
- LA 4794 Sharing Chocolate
大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...
- 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 ...
- UVa 1009 Sharing Chocolate (数位dp)
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVALive - 6952 Cent Savings dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...
- UVALive - 6529 找规律+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...
随机推荐
- MVC 实例详解,蛮好的,适合新手
https://blog.csdn.net/f627422467/article/category/6360437
- MySQL如何获取时间戳
时间戳函数:current_timestamp() 将时间列格式设为timestamp,设定其默认值为CURRENT_TIMESTAMP. 插入一条新纪录,数据库就会自动在时间列存储当前时间.
- python反序列化漏洞
原理在网页源码中如果出现将用户输入数据进行反序列化当成参数输出时,出现漏洞,可造成任意命令执行例如网页源码try: become = self.get_argument('become') ...
- CharacterEncodingFilter详解及源码解析
字符编码过滤器 (Spring框架对字符编码的处理) 基于函数回调,对所有请求起作用,只在容器初始化时调用一次,依赖于servlet容器. web.xml配置文件 <filter> &l ...
- mysql 分组查询入门
- idea安装Maven Helper
1. File -> Settings... 2. 选择 Plunins, 查询 Maven Helper,如果没有,点击 Search in repositories 3. 选择 Maven ...
- C语言备忘录——运算符优先级
丢脸啊,今天写一道算法题,第一次没写对.改了半天愣是没看出来错哪,后面说出了一下过程,突然发现是运算符优先级惹得祸 if (!num % 2){ …… },!的运算优先级高于%,啊啊啊,丧心病狂我找了 ...
- JS - 查找字符串中的某个值,截取其之前。和之后的值
var str = "11:222"; /* * 截取 “ :”之前和之后的值 */document.write(str.split(':')[0]) //输出11doc ...
- windows下移植别人配置好的python环境
一般来说,我们在windows下配置python环境的时候可能会比较推荐用anaconda,那么有一个比较方便的anaconda环境移植方法,也就是说,如果我已经在windows上安装好了anacon ...
- spring boot项目mybatis配置注解+配置文件
maven依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...