K - 背包

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
 
 
这道题可以使用DP做,dp(i)代表i这个数字是否可称,dp(i)=(( dp(j)==1 && 1<j<sum )     j==i + || - 新加入的数字 ?)
 
#include"iostream"
#include"algorithm"
#include"cstring"
using namespace std; const int maxn=; int dp[maxn],ok[maxn],ans[maxn]; int main()
{
int sum,n,temp; while(cin>>n)
{
memset(dp,,sizeof(dp));
dp[]=;
sum=;
for(int i=;i<=n;i++)
{
cin>>temp;
memset(ok,,sizeof(ok));
sum+=temp;
for(int j=;j<=sum;j++) if(dp[j])
{
ok[j+temp]=;
ok[abs(j-temp)]=;
}
for(int j=;j<=sum;j++) if(ok[j])
dp[j]=;
}
int top=;
for(int i=;i<=sum;i++)
{
if(dp[i]==) ans[top++]=i;
}
cout<<top<<endl;
if(top)
{
for(int j=;j<top-;j++) cout<<ans[j]<<" ";
cout<<ans[top-]<<endl;
}
}
return ;
}

你好

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

  1. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  2. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

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

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

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

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

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

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

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

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

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

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  8. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. Veeam对于新病毒防御的建议

    Veeam对于新病毒防御的建议 前言 勒索软件GandCrab 上周末,在我们大家晒娃和欢度六一的时候.勒索软件分发平台 GandCrab 宣布将在一个月内关闭其RaaS(勒索软件即服务)业务平台.据 ...

  2. python中threading模块中的Join类

    join类是threading中用于堵塞当前主线程的类,其作用是阻止全部的线程继续运行,直到被调用的线程执行完毕或者超时.具体代码如下: import threading,time def doWai ...

  3. 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置

    一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...

  4. [USACO 2012 Feb Gold] Cow Coupons【贪心 堆】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=118 传送门2:http://www.lydsy.com/JudgeOn ...

  5. JAVA中abstract,interface,final,static语法

    转自:http://www.cnblogs.com/yueue/archive/2010/04/20/1715863.html 一,抽象类:abstract     1,只要有一个或一个以上抽象方法的 ...

  6. 【先定一个小目标】Asp.net Core 在IIS上的托管运行

    1.安装 .NET Core Framework 下载.net core地址:官网地址 2.Install IIS 在控制面板->程序与功能->Internet Infomation Se ...

  7. get和post中文乱码原理相关博客

    博客一:  http://blog.csdn.net/saygoodbyetoyou/article/details/16834395 博客二: http://www.jb51.net/article ...

  8. 操作JavaScript的Alert弹框

    @Testpublic void testHandleAlert(){ WebElement button =driver.findElement(By.xpath("input" ...

  9. windows系统下如何正确安装Cygwin(图文详解)

    我的操作系统信息是 1.在官网https://cygwin.com/install.html下载win64位安装包 选择包的下载存放目录,点击“下一步”   为了使我们安装的Cygwin能够编译程序, ...

  10. [转][ASP.NET MVC]如何定制Numeric属性/字段验证消息

    本文转自:http://www.cnblogs.com/artech/archive/2012/02/13/NumericPropertyValidation.html 对于一个Numeric属性/字 ...