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 ...
随机推荐
- 项目启动报错:Communications link failure
2017-12-29 10:43:19,776 ERROR [com.alibaba.druid.pool.DruidDataSource] - <init datasource error, ...
- System.Data.SqlClient.SqlException: 'Incorrect syntax near 'OFFSET'.
https://www.nopcommerce.com/boards/t/54586/410-not-running-on-local-system.aspx#209684 Hello, I was ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Stack类
import java.util.Stack ; public class StackDemo{ public static void main(String args[]){ Stack<St ...
- NO8 find结合sed查找替换企业案例多方法精讲&命令总结!
·sed #替换 eg: sed 'sed 's#已有的内容#更改的内容#g' oldboy.txt s 代表替换,g代表全局,sg就是全局替换 ...
- win10下pip3安装tesserocr时报错
使用pip3在线安装tesserocr时报错,刚开始报错内容是提示未安装vs2014,安装完以后报错内容如下 ERROR: Command errored out with exit status 1 ...
- LeetCode实战练习题目 - Array
实战练习题目 - Array 盛最多水的容器 class Solution { public: int maxArea(vector<int>& height) { int res ...
- Golang的运算符-比较运算符
Golang的运算符-比较运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.比较运算符概述 比较运算符也称为关系运算符,比较运算符返回的类型为bool类型,常见的比较运算符 ...
- 全局保存ajax请求到的数据
var menuJson = (function() { var result; $.ajax({ type: 'get', u ...
- 机器学习(ML)一之 Linear Regression
一.线性回归 1.模型 2.损失函数 3.优化函数-梯度下降 #!/usr/bin/env python # coding: utf-8 import torch import time # init ...
- css选择器权重、样式继承、默认样式
学过css的小伙伴都是指css选择器的权重 !important Infinity 行间样式 1000 id 100 class|属性|伪类 10 标签|伪元素 1 通配符 0 权重相同 相同cs ...