Divideing Jewels

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述

Mary and Rose own a collection of jewells. They want to split the collection among themselves so that both receive an equal share of the jewels. This would be easy if all the jewels had the same value, because then they could just split the collection in half. But unfortunately, some of the jewels are larger, or more beautiful than others. So, Mary and Rose start by assigning a value, a natural number between one and ten, to each jewel. Now they want to divide the jewels so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the jewels in this way (even if the total value of all jewels is even). For example, if there are one jewel 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 jewels.

 
输入
Each line in the input file describes one collection of jewels to be divided. The lines contain ten non-negative integers n1 , . . . , n10 , where ni is the number of jewels of value i. The maximum total number of jewells will be 10000. 
The last line of the input file will be "0 0 0 0 0 0 0 0 0 0"; do not process this line. 
输出
For each collection, output "#k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.". 
Output a blank line after each test case. 
样例输入
1 0 1 2 0 0 0 0 2 0
1 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
样例输出
#1:Can't be divided.

#2:Can be divided.

解题思路:多重背包板子题~马丹!!!~写了那么久~对于多重背包,可以先对每种物品判断是否超出背包容量,1.如果该种物品超出,则可以当做完全背包来做,因为完全背包不就是装满该容量的背包,物品可以任意取嘛,都一样的。2.如果没有超出,则对于该种物品来说,当做01背包来做,但是这里将该种物品又进行了捆绑分组(2进制优化,时间复杂度可降到O(V*Σlog(n[i]))),模板题~
#include<stdio.h>
using namespace std;
#define max(a,b) (a)>(b)?(a):(b);
const int maxn=100100;
int dp[maxn];
int w[100],c[100],num[100];
int V;
void ZeroOnePack(int cost,int weight){
for(int i=V;i>=cost;i--){
dp[i]=max(dp[i],dp[i-cost]+weight);
}
}
void CompletePack(int cost ,int weight){
for(int i=cost;i<=V;i++){
dp[i]=max(dp[i],dp[i-cost]+weight);
}
}
void MultiplePack(int cost ,int weight,int amount){
if(cost * amount>=V){
CompletePack(cost,weight);
return ;
}
//如果amount为13
int k=1;
while(k<amount){
ZeroOnePack(k*cost,k*weight); //则k分别为1,2,4
amount-=k;
k*=2;
}
ZeroOnePack(amount*cost,amount*weight);//这里amount为6
}
int main(){
int n,i,j,k,sum,cnt=0;
while(1){
n=10;
sum=0;
for(i=1;i<=n;i++){
scanf("%d",&num[i]);
c[i]=w[i]=i;
sum+=num[i]*i;
}
if(sum==0)
break;
if(sum&1){ //如果为宝物的总价值为奇数,必然不能平分。优化
printf("#%d:Can't be divided.\n",++cnt);
continue;
}
V=sum/2;
for(i=1;i<=n;i++){
MultiplePack(c[i],w[i],num[i]);
}
if(sum-2*dp[V]){
printf("#%d:Can't be divided.\n",++cnt);
}else{
printf("#%d:Can be divided.\n",++cnt);
}
printf("\n");
}
return 0;
}

  



nyoj 546——Divideing Jewels——————【dp、多重背包板子题】的更多相关文章

  1. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  2. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  3. HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)

    HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...

  4. hihoCoder #1038 : 01背包(板子题)

    #1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...

  5. hdu 2191 珍惜现在,感恩生活 多重背包入门题

    背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...

  6. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  7. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

  8. Buns(dp+多重背包)

    C. Buns time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

随机推荐

  1. 数值限制------c++程序设计原理与实践(进阶篇)

    每种c++的实现都在<limits>.<climits>.<limits.h>和<float.h>中指明了内置类型的属性,因此程序员可以利用这些属性来检 ...

  2. Kotlin 在kotlin内使用Java的一些注意(长篇)

    首先呢,大部分的java在kotlin内是可以使用的. 但是有些java的关键字和kotlin的一样,而意义不一样就需要转义.(单引号括起来的)这一点需要注意. 这是一个长篇 我会不断更新.毕竟我也在 ...

  3. underscore里面的debounce与throttle

    throttle 策略的电梯.保证如果电梯第一个人进来后,15秒后准时运送一次,不等待.如果没有人,则待机. debounce 策略的电梯.如果电梯里有人进来,等待15秒.如果又人进来,15秒等待重新 ...

  4. 【洛谷1685】游览 拓扑排序+DP

    题目描述 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!!于是你还想乘船从桃 ...

  5. Exadata 18.1新特性--云平台存储节点升级

    1.传统方式的存储节点升级流程: (1).将存储节点升级包下载到数据库服务器,通常是DB01上. (2).解压缩存储节点升级包. (3).用升级包中的patchmgr工具滚动或非滚动地升级每个存储节点 ...

  6. Little Elephant and Magic Square

    Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains s ...

  7. POJ_1850 Code【组合的运用】

    题目: Transmitting and memorizing information is a task that requires different coding systems for the ...

  8. BZOJ - 1458 / P4311 最大流应用 贪心

    题意:给定n*m的图,每个士兵可以占领当前行和列,第i行至少要R[i]个士兵占领,第j列至少要C[j]个士兵占领,部分网格无法占领,求占领所用最少士兵数,若无解则输出orz 士兵的贡献情况有1(只有效 ...

  9. SQL数据库查询一列数据返回一行

    SQL:数据库合并列数据:遇到一个更新的问题 想要把查询到的数据某一列拼接成字符串形式返回用的是SQL数据库中的STUFF函数比如 查询到的表(u_College)如下Id Name Age Clas ...

  10. 解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题

    解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题 div{ width: 100%; overflow-y: hidden; -webkit-o ...