zoj 1149 Dividing
1到6的卡分别各有有限制的张数,问能不能恰好分,总张数不能超过20000.
很明显是多重背包问题,上去果写了个三重循环,然后就T了,重新打开背包九讲,找到了多重背包的二进制拆分优化,把其中一维n的复杂度简化为logn的复杂度。
二进制拆分优化:就是1,2,4,2^k(满足和小于分解数最大的k),二进制优化可行的原因,因为可以用二进制数表示任意不同小于等于k的数。
注意:数组不要开小,RE了
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
int a[7];
int dp[60005];
int sum=0;
int num[60006];
int kk;
void bin(int n,int k) //多重背包的二进制拆分
{
int i,x;
for(i=0; ; i++)
{
x = 1<<i;
if(k<x)
break;
k-=x;
num[kk++] = x*n;
}
if( k != 0 )
num[kk++] = n*k;
}
int main()
{
// freopen("input.txt","r",stdin);
int cas=1;
while(1)
{
memset(num,0,sizeof(num));
memset(dp,0,sizeof(dp));
kk=0;
sum=0;
for(int i=1;i<7;i++)
{
scanf("%d",&a[i]);
sum+=a[i]*i;
bin(i,a[i]);
}
if(!sum)
break;
printf("Collection #%d:\n",cas++);
if(sum%2==1)
{
printf("Can't be divided.\n\n");
continue;
}
sum/=2;
// printf("%d\n",kk);
for(int i=0;i<kk;i++)//01
for(int k=sum;k>=num[i];k--)
{
dp[k]=max(dp[k],dp[k-num[i]]+num[i]);
}
if(dp[sum]==sum)
{
printf("Can be divided.\n\n");
}
else
{
printf("Can't be divided.\n\n");
}
}
}
zoj 1149 Dividing的更多相关文章
- 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 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
随机推荐
- android onIntent 是什么东西
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...
- jquery初学者易犯的错误
1 获取类或者对象的时候,忘记写“#”或者“.” 错误案例: $(document).ready(function(){ $("btn1").click(function(){ a ...
- Mongoose的使用
最近想做一个练手的App小项目.考虑到数据接口的问题,因为关系型数据库用的比较多,也有一定经验了,所以打算使用比较火的MongoDB作为数据库,下面就介绍一下Mongoose的使用方法吧. 概念:Mo ...
- Convert Sorted List to Binary Search Tree java
public TreeNode sortedListToBST(ListNode head) { if(head==null) return new TreeNode(0); ArrayList< ...
- Java 基础之 static 静态
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...
- (原)android的JNI中使用C++的类
android的JNI代码中可以调用C++的类,但是不能直接调用,要加上一个类似于接口的java类,这个类内部调用C++的类.实际上和接口类直接调用C++中的函数差不多,只是稍微复杂了一点. 1. 写 ...
- SimpleDateFormat的使用
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text -> date)和标准化. 下面是个小 ...
- PC远程调试移动设备(转载)
我们在移动端进行前端开发时,会遇到一个让人头痛但不得不面对的问题--调试. 在 PC 机器上,我们有功能强大的 Chrome DevTools.Firebug,即便是老版本的 IE ,我们也可以安装微 ...
- RecyclerView不同类型Item的展示
代码如下: public class AccessoiresAdapter extends RecyclerView.Adapter { final int VIEW_TYPE_ACCESSORY = ...
- Elevator(hdoj 1008)
Problem Description The highest building in our city has only one elevator. A request list is made u ...