Dividing (多重背包 搜索)
/
第一个多重背包题目 真的不理解二进制优化
/http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=10594&pid=1001&ojid
以下是用二进制优化的 不会超时
include
include
include
include
using namespace std;
int dp[200000],a[7];
void zeroOne(int weiht,int value,int c)
{
for(int j=c;j>=weiht;j--)
dp[j]=max(dp[j],dp[j-weiht]+value);
}
void complelet(int weight,int value,int c)
{
for(int j=weight;j<=c;j++)
dp[j]=max(dp[j],dp[j-weight]+value);
}
void duoChong(int weight,int value,int count,int c)
{
if(countvalue>=c)complelet(weight ,value,c);
else
{
int k=1;
while(k<count)
{
zeroOne(kweight,kvalue,c);
count-=k;
k+=k;
}
zeroOne(countweight,countvalue,c);
}
}
int main()
{
int count=0;
while(1)
{
int sum=0;
for(int i=1; i<=6; i++)
{
scanf("%d",&a[i]);
sum+=a[i]i;
}
if(sum==0)break;
printf("Collection #%d:\n",++count);
if(sum%2==1)
{
printf("Can't be divided.\n\n");
continue;
}
int c=sum/2;
memset(dp,0,sizeof(dp));
for(int i=1; i<=6; i++)
duoChong(i,i,a[i],c);
if(dp[c]==sum/2)printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return 0;
}
直接化为01背包计算 超时
include
include
include
include
using namespace std;
int dp[200000],a[7],w[7];
int main()
{
int count=0;
while(1)
{
int sum=0;
for(int i=1; i<=6; i++)
{
scanf("%d",&a[i]);
sum+=a[i]*i;
w[i]=i;
}
if(sum==0)break;
printf("Collection #%d:\n",++count);
if(sum%2==1)
{
printf("Can't be divided.\n\n");
continue;
}
int c=sum/2;
memset(dp,0,sizeof(dp));
for(int i=1; i<=6; i++)
for(int k=1; k<=a[i]; k++)//有限个数量(超时)
for(int j=c; j>=w[i]; j--)
dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
printf("sum/2=%d\n",dp[sum/2]);
if(dp[c]==sum/2)printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return 0;
}
下面是搜索做的 比多重背包快了很多
include
include
include
include
using namespace std;
int op=0;
int mid;
int a[8];
void dfs(int cnt,int sum)
{
if(op)return;
if(sum==mid)
{
op=1;
return ;
}
for(int i=cnt; i>=1; i--)
{
if(a[i])
if(sum+i<=mid)
{
a[i]--;
dfs(i,sum+i);
}
}
}
int main()
{
int count=0;
while(1)
{
int sum=0;
for(int i=1; i<=6; i++)
{
scanf("%d",&a[i]);
sum+=i*a[i];
}
if(sum==0)break;
printf("Collection #%d:\n",++count);
if(sum%2==1)
{
printf("Can't be divided.\n\n");
continue;
}
mid=sum/2;
op=0;
dfs(6,0);
if(op)printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return 0;
}
Dividing (多重背包 搜索)的更多相关文章
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Dividing 多重背包 倍增DP
Dividing 给出n个物品的价值和数量,问是否能够平分.
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- hdu1059 Dividing ——多重背包
link:http://acm.hdu.edu.cn/showproblem.php?pid=1059 最简单的那种 #include <iostream> #include <cs ...
- R - Dividing 多重背包
来源poj1059 Marsha and Bill own a collection of marbles. They want to split the collection among thems ...
随机推荐
- java 基本类型和包装类的比较
public class BoxingTest { @Test public void test1(){ String a = new String("1"); String b ...
- LENGTH和LENGTHB函数,substrb截取也是同一个道理。
oracle 利用 LENGTH和LENGTHB函数区分中英文(2009-02-07 10:49:29) 转载▼ 标签: it 分类: oracle 前一段时间,我一朋友问我怎么得出这个字符串是中文还 ...
- linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>
运用Linq,将List<T> 转换为 Dictionary<T Key,T Value> 即:List<T> ToDictionary<T Key,T V ...
- hibernate4.3.8整合struts2过程中遇到的问题
1.遇到的异常: Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to ...
- index full scan/index fast full scan/index range scan
**************************1************************************* 索引状态: valid. N/A . ...
- 【转】 KVC/KVO原理详解及编程指南
原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...
- 苹果App store 2015最新审核标准公布(2015.3)
苹果近日更新了AppStore审核指南的相关章节,对此前版本进行了修改和完善.除了增加应用截图.预览等限制外,使用ApplePay进行定期付款的应用程序必须展示每个阶段所需款额,费用归属以及如何取消. ...
- [转]Delphi I/O Errors
The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...
- 阿里druid 介绍及配置
1. 简介,什么是Druid Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器组成.该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求,比如 ...
- SGU 131.Hardwood floor
时间限制:0.25s 空间限制:4M 题意: 给出 n*m (1≤n.m≤9)的方格棋盘,用 1*2 的矩形的骨牌和 L 形的(2*2 的 去掉一个角)骨牌不重叠地覆盖,求覆盖满的方案数. Solut ...