Problem Description
Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.
Input
The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.
Output
For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.
Sample Input

3  
1 2 4  
3  
9 2 1

Sample Output

0  
2  
4 5

解题思路:

用天平称重时,可以有两种方法。一种是砝码和物体分别放在两侧;

另一种是砝码和物体放在同一侧,另一侧再放砝码,此时和物体在同一侧的砝码相当于是负重量,这是本题的关键

首先,砝码重量可能相等,要统计各自的数量

然后再套用母函数

最后,统计不能称量的

程序代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define W 10005
using namespace std;
int b[W],c[W];
int main()
{
int a[],d[];
int N,i,t,n,j,len,r,p,k;
while(scanf("%d",&N)!=EOF)
{
for(i=;i<N;i++)
scanf("%d",&a[i]);
sort(a,a+N);
d[]=t=a[];n=;j=;
for(i=;i<N;i++)
{
if(a[i]==t){n++;}
else {d[j]=n;n=;a[j++]=t;t=a[i];}
}
a[j]=t;
d[j]=n;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(i=;i<=d[]*a[];i+=a[])
c[i]=;len=d[]*a[];
for(i=;i<=j;i++)
{
for(p=;p<=len;p++)
for(k=;k<=d[i]*a[i];k+=a[i])
{
b[p+k]+=c[p];
if(k!=&&p!=)
{r=fabs(p-k);
b[r]+=c[p];}
}
len+=d[i]*a[i];
for(k=;k<=len;k++)
{c[k]=b[k];b[k]=;}
}
n=;
for(i=,k=;i<=len;i++)
if(c[i]==) b[k++]=i;
if(k==)printf("0\n");
else {
printf("%d\n",k);
for(i=;i<k;i++)
{ if(i!=)printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
}
return ; }

动态规划——K背包问题的更多相关文章

  1. [Dynamic Programming]动态规划之背包问题

    动态规划之背包问题 例题 现有4样物品n = ['a', 'b', 'c', 'd'],重量分别为w = [2, 4, 5, 3],价值分别为v = [5, 4, 6, 2].背包最大承重c = 9. ...

  2. 记录结果再利用的"动态规划"之背包问题

    参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...

  3. 动态规划_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50328141 本文出自:[梁敬明的博客] 1.动态规划 什么是动态规划?动态规划就是将 ...

  4. 【动态规划/多重背包问题】POJ1014-Dividing

    多重背包问题的优化版来做,详见之前的动态规划读书笔记. dp[i][j]表示前i中数加得到j时第i种数最多剩余几个(不能加和得到i的情况下为-1)递推式为: dp[i][j]=mi(dp[i-1][j ...

  5. 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】

    任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...

  6. python 动态规划(背包问题和最长公共子串)

    背包问题 现在要往一个可以装4个单位重量的背包里怎么装价值最高:A重量1个单位,价值15:B重量3个单位,价值20:C重量4个重量,价值30 使用动态规划填充空格 class SolutionBag: ...

  7. 算法设计(动态规划实验报告) 基于动态规划的背包问题、Warshall算法和Floyd算法

    一.名称 动态规划法应用 二.目的 1.掌握动态规划法的基本思想: 2.学会运用动态规划法解决实际设计应用中碰到的问题. 三.要求 1.基于动态规划法思想解决背包问题(递归或自底向上的实现均可): 2 ...

  8. poj 1742 Coins (动态规划,背包问题)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32977   Accepted: 11208 Descripti ...

  9. 九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)

    题目地址:http://ac.jobdu.com/problem.php?pid=1455 题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元, ...

随机推荐

  1. 【转】Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

  2. CSS3 box-sizing 属性

    定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...

  3. HTML5 文件域+FileReader 分段读取文件(四)

    一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...

  4. [o] SQLite数据库报错: Invalid column C

    向SQLite数据库内新增列,之前出现过报错为提示no such column,通过删除并重建数据库文件解决,这次报错为无效的数据列: java.lang.IllegalArgumentExcepti ...

  5. ImageView 设置OnTouchListener

    ImageView的OnTouchListener,onTouch方法要返回true,MotionEvent.ACTION_UP,MotionEvent.ACTION_MOVE 才有效. 其实关于返回 ...

  6. How to customize authentication to my own set of tables in asp.net web api 2?

    ssuming your table is called AppUser, convert your own AppUser domain object to IUser(using Microsof ...

  7. 实现HTTP跳转到HTTPS

    1 首先在您的网站下新建一个站点,名称随意,在属性中分配TCP端口为80,SSL不分配 然后在属性>主目录下配置 将此资源的内容来自: 改为 重定向到URL 然后重定向到中  输入:  HTTP ...

  8. cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能

    cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画 一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部 ...

  9. 【POJ2352】【树状数组】Stars

    Description Astronomers often examine star maps where stars are represented by points on a plane and ...

  10. Android开发系列----学习伊始

    因为对移动端开发开始感兴趣,开始学习App开发,没有苹果环境的我,只好先选择Android来玩一玩了~~ 找了一套视频,买了几本java.android开发的书,开始搞起~~