HDU 1059(多重背包)
题意描述
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.
思路
一道多重背包的问题,先计算所有的和,如果不能被平分,直接输出。否则就求dp[sum/2]是否等于sum/2,如果相等就说明可以平分。
AC代码
#include<iostream>
#include<string>
#include<cstring>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=15;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
int a[7],dp[M],v[M];
int Case=1;
void solve(){
while(cin>>a[1]){
cin>>a[2]>>a[3]>>a[4]>>a[5]>>a[6];
mst(dp,0);
int sum=0;
rep(i,1,7) sum+=a[i]*i;
if(sum==0) break;
if(sum%2!=0) printf("Collection #%d:nCan't be divided.nn",Case++);
else{
int cnt=1;
rep(i,1,7){
int k=1;
while(k<=a[i]){
v[cnt++]=k*i;
a[i]-=k;
k*=2;
}
if(a[i]>0){
v[cnt++]=i*a[i];
}
}
int n=cnt;
rep(i,1,n){
rrep(j,sum/2,v[i]){
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
}
}
if(dp[sum/2]!=sum/2) printf("Collection #%d:nCan't be divided.nn",Case++);
else printf("Collection #%d:nCan be divided.nn",Case++);
}
}
}
int main(){
//IOS;
solve();
return 0;
}
- 《Spring敲门砖之基础教程第一季》 第二章(1) Spring框架之IOC首例-HelloWorld
- Java9中的GC调优基础
- javascript深入理解js闭包
- https连接的前几毫秒发生了什么
- android自定义view实现progressbar的效果
- 还是不靠谱!多维LSTM网络预测比特币价格【机器学习应用区块链系列二】
- picasso图片缓存框架
- Git基础和规范-协同开发
- JavaScript 知识点梳理 | 从基础语法到高级用法
- Java正则速成秘籍(一)之招式篇
- Rafy 框架 - 使用 SqlTree 查询
- 详细讲解Quartz.NET
- android AlarmManager讲解
- Intent和PendingIntent的区别
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android WebView userAgent 设置为桌面UA实例
- Android WebView 内处理302重定向不跳转的解决
- android studio集成极光推送的操作步骤
- Android中的webview监听每次URL变化实例
- 解决Android WebView拦截url,视频播放加载失败的问题
- Android 极光推送别名与标签方式
- Android Webview的postUrl与loadUrl加载页面实例
- android Web跳转到app指定页面并传递参数实例
- Dialog全屏,去掉状态栏的方式
- Android 通过TCP协议上传指定目录文件的方法
- Android webview加载https链接错误或无响应的解决
- Android Manifest中meta-data扩展元素数据的配置与获取方式
- Android 配置gradle实现VersionCode自增实例
- 解决Android Studio Gradle Metadata特别慢的问题
- Android ProgressDialog用法之实现app上传文件进度条转圈效果