【HDU 1059】 Dividing 多重背包问题 DP

解决一个经典的数学问题:如何将不同价值的硬币均分成两份等价的集合。使用多重背包算法,通过动态规划检查是否能从给定的硬币集合中找到一半总价值的组合。

Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0’’. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0’’; do not process this line.

Output
For each colletcion, output Collection #k:'', where k is the number of the test case, and then eitherCan be divided.’’ or ``Can’t be divided.’’.

Output a blank line after each test case.

Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0

Sample Output
Collection #1:
Can’t be divided.

Collection #2:
Can be divided.

题意:给你1-6枚面值的硬币各若干,问能否均分成两份等价的

思路:

多重背包问题。
1.用dp[i]来表示能否凑到i这么多的硬币(目的为了凑到sum/2)
2.从小到大遍历面值,内层遍历一遍j从1->sum/2,每次拿一枚硬币去铺到j,看能否铺成功。条件就是dp[j]是没有铺到的,且dp[j-i]是要铺到的(因为要从dp[j-i]这个状态转移过来),同时在j-i的时候我当前i剩下的硬币数量要>=1 。状态转移方程如下:

`  rep(i,1,6)
        {
            rep(j,0,sum/2) Left[j] = a[i];  
            rep(j,i,sum/2) if(!dp[j]&&dp[j-i]&&Left[j-i]>=1)    dp[j] = 1, Left[j] = Left[j-i] - 1;
        }`

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int a[10];
int dp[maxn];
int Left[maxn];

int main()
{
    int num = 1;
    while(1)
    {
        rep(i,1,6) a[i] = read();
        if(!(a[1]||a[2]||a[3]||a[4]||a[5]||a[6])) break;
        int sum = 0; mem(dp,0);
        rep(i,1,6) sum += a[i]*i;
         printf("Collection #%d:\n", num++);
        if(sum%2)
        {
             puts("Can't be divided.\n");
            continue;
        }
        dp[0] = 1;
        rep(i,1,6)
        {
            rep(j,0,sum/2) Left[j] = a[i];
            rep(j,i,sum/2) if(!dp[j]&&dp[j-i]&&Left[j-i]>=1)    dp[j] = 1, Left[j] = Left[j-i] - 1;
        }
        if(dp[sum/2])   puts("Can be divided.\n");
        else     puts("Can't be divided.\n");
    }
    return 0;
}

HLC-723G8全自动糖化血红蛋白分析仪的中文使用说明书。第 1 章、前言1、 1 概要……………………………….…………………………………………………………P1-1第 2 章、使用之前须知2、 1 零部件的检查…………………………………………………………….…………………P2-22、 2 仪器的构成………………………………………………………………………………….P2-52、 3 各部分的名称和作用………….……………………………………………………………P2-72、 4 设置地点…………….…….…………………………………….…………………………P2-132、 5 连接………………..….……………………………………………………………………P2-182、 6 柱………...…………………………………………………………………………………P2-23第 3 章、测定操作3、 1 测定的原理…………………………………………………………….…………………P3-13、 2 接通电源……………………………………………………………………………………P3-23、 3 测定的流程………………………………………………………………….………………P3-83、 4 了解仪器的工作状态………………………………………………………...……………P3-103、 5 测定前的确认事项………..……………………………………………….………………P3-163、 6 校准………………………………………………………………………………………P3-213、 7 试样………………………………………………………………………………………P3-293、 8 测定的开始和结束………………………………………………………………………P3-393、 9 消除错误…………………………………………………………………………………P3-423、 10 紧急试样的测定(STAT) ……………………………………………………………... P3-443、 11 利用 POWER 键关闭电源操作…………………………………..……………………P3-473、 12 测定结果的看法…………………………………………………………………..……P3-483、 13 清单数据…………………………………………………………..……………………P-3-55第 4 章、画面操作4、 1 主画面……………………………………………………………………………… P4-14、 2 STAT………………………………………………………………………………P4-44、 3 维护………………………………………………………………………………… P4-64 、 4 试 剂 更 换 … … … … … … … … … … … … … … … … … … … … … … … . P 4 - 74、 5 菜单………………………………………………………………………………… P4-84、 6 参数设定…………………………………………………………………………P4-94、 7 存储卡……………………………………………………………………………… P4-184、 8 保存数据的一览表………………………………………………………………P4-214、 9 被保存的测定结果的确认、重算、向主机再次传送……………………… P4-234、 10 日期、时间的设定和一周定时的设定………………………………………….. P4-25I4、 11 清单数据显示和条形码的编辑…………………………………………………... P4-274、 12 应用程序………………………………………………………………………P4-294、 13 数据通信的设定………………………………..……………………………………P4-304、 14 密码输入(参数初始化) …………………………………………………………P4-324、 15 参数的打印……………………………………………………………………P4-334、 16 注解行的设定…………………………………………………………………P4-344、 17 记录文件的确认………………………………………………………………P4-354、 18 结果判定参数的设定…………………………………………………………..P4-374、 19 条型码读取器的设定和读取检查………………………..……………………….P4-42第 5 章、日常的维护保养管理5、 1 每天的保养和管理………………………………………………………………P5-15、 2 维护保养检查项目………………………………………………………………P5-25、 3 洗脱液、溶血清洗液的更换……………………………………………………P5-45、 4 洗脱液的注入……………………………………………………………………P5-85、 5 泵的排气…………………………………………………………………………P5-105、 6 柱的更换…………………………………………………………………………P5-135、 7 过滤器的更换……………………………………………………………………P5-165、 8 打印纸的更换……………………………………………………………………P5-195、 9 吸管过滤器的更换………………………………………………………………P5-215、 10 采样针的更换………………………………………………………………… P5-225、 11 针清洗部件 O 型环的更换…………………………………………………….P5-25第 6 章、故障检修6、 1 在测定中的注意事项……………………………………………………………..P6-16、 2 故障时要确认的事项………………………………………………………………….P6-36、 3 错误信息……………………………………………………………………….…..P6-66、 4 异常色谱………………………………………………………………………P6-22第 7 章、附录7、 1 从软盘读取文件…………………..………………………………………………….P7-17、 2 与主计算机之间的通信………………………………………………………………… P7-6.7、 3 主机的设计规格…………………………………………………………….………… P7-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值