HDU OJ Dividing 题目1059
2015-08-02 20:46
417 查看
多重背包问题
#include <iostream>
#include <string.h>
#include <stdio.h>
using
namespace std;
int mar[7],f[120005],v,k;
void ZeroOnePack(int va,int
c
{
for(int i
= v ; i
>= va; i--)
if(f[i-va]+c>f[i])
f[i] = f[i-va]+c;
}
void complatePack(int va,int
c)
{
for(int i
= va; i
<= v; i++)
if(f[i-va]+c>f[i])
f[i] = f[i-va]+c;
}
void mutilPack(int va,int
c,int n)
{
if(n*va>=v)
complatePack(va,c);
else
{
int count
=
1;
while(count<n)
{
ZeroOnePack(count*va,count*c);
n -=count;
count =2*count;
}
ZeroOnePack(n*va,n*c);
}
}
int main()
{
int total,num=1;
while(1)
{
total =
0;
for(int i
=
1; i<7;i++)
{
scanf("%d",&mar[i]);
total += i*mar[i];
}
if(total==0)break;
if(total%2)
{
printf("Collection #%d:\nCan't be divided.\n\n",num);
num++;
continue;
}
v = total/2;
memset(f,0,sizeof(f));
for(int i=1;i<7;i++)
mutilPack(i,i,mar[i]);
if(f[v]==v) printf("Collection
#%d:\nCan be divided.\n\n",num);
else printf("Collection #%d:\nCan't be divided.\n\n",num);
num++;
}
return
0;
}
多重背包问题
#include <iostream>
#include <string.h>
#include <stdio.h>
using
namespace std;
int mar[7],f[120005],v,k;
void ZeroOnePack(int va,int
c
{
for(int i
= v ; i
>= va; i--)
if(f[i-va]+c>f[i])
f[i] = f[i-va]+c;
}
void complatePack(int va,int
c)
{
for(int i
= va; i
<= v; i++)
if(f[i-va]+c>f[i])
f[i] = f[i-va]+c;
}
void mutilPack(int va,int
c,int n)
{
if(n*va>=v)
complatePack(va,c);
else
{
int count
=
1;
while(count<n)
{
ZeroOnePack(count*va,count*c);
n -=count;
count =2*count;
}
ZeroOnePack(n*va,n*c);
}
}
int main()
{
int total,num=1;
while(1)
{
total =
0;
for(int i
=
1; i<7;i++)
{
scanf("%d",&mar[i]);
total += i*mar[i];
}
if(total==0)break;
if(total%2)
{
printf("Collection #%d:\nCan't be divided.\n\n",num);
num++;
continue;
}
v = total/2;
memset(f,0,sizeof(f));
for(int i=1;i<7;i++)
mutilPack(i,i,mar[i]);
if(f[v]==v) printf("Collection
#%d:\nCan be divided.\n\n",num);
else printf("Collection #%d:\nCan't be divided.\n\n",num);
num++;
}
return
0;
}
相关文章推荐
- Python学习之函数
- 每天一个小知识点21(拖拽效果)
- Asp.net-知识总结(2)
- 【DB.MySQL】在Mac如何启动MySQL
- 虚幻3引擎-多线程渲染机制
- scrapy学习笔记--解析结果存储
- 调用startActivityForResult后onActivityResult立刻响应,返回当前页onActivityResult不响应的问题
- kiki's game
- 我多希望我学编程时,有人教我这些事!
- 拉格朗日乘数法
- 如何高性能布局
- 关于System V 信号量
- Android开发经典笔试面试题汇总(持续更新中)
- 敏捷开发下该深度思考的三个问题
- Shell 学习14 - Shell printf 命令
- hdoj 最简单的计算机
- ViewPager+Fragment问题
- 【剑指Offer面试题】 九度OJ1369:字符串的排列
- Taglib
- 学习笔记开始了,加油!