集训第五周动态规划 K题 背包

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

题意,给你一些砝码,如3个砝码1 2 4,要求你用这些砝码称出从1到砝码之和的重量,其中不能称出的重量就要输出出来

很显然1 2 4可以称出从1到7的所有重量,输出0

9 2 1首先按左物右码可以输出1 2 3 10 11 12,而6 7 8可以是6+1=2=9,7+2=9,8+1=9

所以输出4 5

解法:可以使用DP做,dp(i)代表这个i值是否可以称出来,0代表这个可以称,1代表不可以,然后初始赋值为dp(0)=0显然重量为0是可称的,后来的判断也要先以此为基石,再把其他的所有dp(i)赋值为1,代表这个数还没开始测试,当然是不可称的状态,然后再开始具体工作

1.先按左物右码的原理开始模拟,测出那些可以由此原理测出的数字

dp(j)=min{dp(j-a(i)),dp(j)}

2.再按照右边放一个砝码,左边往待测物体上加砝码开始测试

dp(j)=min{dp(j+a(i)),dp(j)}

最后再输出那些值为1的序号

#include"iostream"
#include"cstring"
using namespace std;
int a[101];
int book[10001],ans[10001];
int m,n;

void Work()
{
     for(int i=0;i<n;i++)
     for(int j=m;j>=a[i];j--)
     {
     book[j]=min(book[j],book[j-a[i]]);
     }

     for(int i=0;i<n;i++)
     for(int j=0;j<=m-a[i];j++)
     {
     book[j]=min(book[j],book[j+a[i]]);
     }

     int top=0;
     for(int i=1;i<=m;i++)
     if(book[i]==1) ans[top++]=i;

     cout<<top<<endl;
     if(top)
     {
     for(int j=0;j<top-1;j++) cout<<ans[j]<<" ";
     cout<<ans[top-1]<<endl;
     }
}

int main()
{
    while(cin>>n)
    {
     m=0;
     memset(book,0,sizeof(book));
     for(int i=0;i<n;i++)  {cin>>a[i];m+=a[i];}
     for(int j=1;j<=m;j++) book[j]=1;
     book[0]=0;
     Work();
    }
    return 0;
}

O(O—O)O

时间: 2024-10-07 13:17:59

集训第五周动态规划 K题 背包的相关文章

集训第五周动态规划 I题 记忆化搜索

Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.在上面的例子

集训第五周动态规划 F题 最大子矩阵和

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this probl

集训第五周动态规划 J题 括号匹配

Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regul

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

集训第五周动态规划 G题 回文串

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

2018暑假集训第五周感想

第五周有点漫长..题打得有点磨,急躁,自卑等等负面情绪不断出来(ㄒoㄒ) 线段树真难,dp也真难..如果线段树是有思路实现不了,dp就是完全没思路,核心思想就是找一个转移方程,然而ヽ(´¬`)ノ 寻找dp的转移方程真是一个艰难的过程,同时还伴随着恐怖的状态压缩,也没有什么固定的套路和方法,只能靠多练习和领悟了(?•ω•?) dp也就是动态规划是针对一类最优解的算法,核心思想是类似分治,把一个问题分解成若干个子问题,通过每一个子问题的最优决策得到最优解(- ̄▽ ̄)- dp的实现有递推,也有记忆化搜

集训第五周D题 LCS

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa

第十五周oj刷题——Problem K: C++习题 复数类--重载运算符+,-,*,/

Description 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符重载函数作为Complex类的成员函数.编写程序,分别求两个复数之和.差.积和商. Input 两个复数 Output 两个复数之和.差.积和商 Sample Input 3 4 5 -10 Sample Output c1+c2=(8.00,-6.00i) c1-c2=(-2.00,14.00

程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8. ijAl- 9. ijAl-mnop 10. qrst-abcd- 11. abcd-qrst-abcd- uvw xyz about big me take abcd qrst-abcd- 要 求:MyString类必须是从C++的标准类string类派生而来.提示1:如果将程序中所有 "My