The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5956    Accepted Submission(s): 2427

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
 
 
一条挺有意思的递推。
就是问给出n个重量为wi的砝码。
问你不能称出的重量有哪些。
bool  dp[i][j] 表示前i个砝码, 能否称出重量 j 。
 
初始化dp[0][0] = true ;
对于一个 dp[i-1][j] = true , 必然有 dp[i][j+a[i]] = true , dp[i][abs(j-a[i])] = true 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
typedef pair<int,int>pii;
#define X first
#define Y second
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = 1e9+;
bool dp[][N];
int n , m , a[N] , b[N] , tot ;
void init() {
memset( dp , false ,sizeof dp );
dp[][] = true ;
for( int i = ; i <= n ; ++i ){
for( int j = ; j <= tot ; ++j ){
if( dp[i-][j] ) dp[i][j] = dp[i-][j];
if( dp[i-][j] ){
dp[i][j+a[i]] = true ; dp[i][abs(j-a[i])] = true ;
}
}
// for( int j = 0 ; j <= tot ; ++j ) cout << dp[i][j] << ' '; cout << endl ;
}
}
void Run() {
tot = ;
for( int i = ; i <= n ; ++i )
cin >> a[i] , tot += a[i];
init(); int cnt = ;
for( int i = ; i <= tot ; ++i ) if( !dp[n][i] ){
b[cnt++] = i ;
}
cout << cnt << endl ;
for( int i = ; i < cnt ; ++i )
cout << b[i] << (i+==cnt?"\n":" "); }
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
while( cin >> n ) Run();
}

HDU 1709 The Balance( DP )的更多相关文章

  1. 组合数学 - 母函数的运用 --- hdu 1709 :The Balance

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 1709 The Balance(母函数)

    题意: 有一个天平.有N个砝码.重量分别是A1...AN. 问重量[1..S]中有多少种重量是无法利用这个天平和这些砝码称出来的. S是N个砝码的重量总和. 思路: 对于每一个砝码来说,有三种:不放, ...

  3. hdu 1709 The Balance

    母函数的特殊情况,左右两边都可以放,如样例1,2,9 母函数为(1+x+1/x)*(1+x^2+1/x^2)*(1+x^9+1/x^9) 化简为(1+x+x^2)*(1+x^2+x^4)*(1+x^9 ...

  4. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  7. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  8. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  9. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

随机推荐

  1. eclipse不小心删除文件如何恢复

    转自:https://blog.csdn.net/u012129031/article/details/78791277 1.右键点击java项目工程名,选择restort from history, ...

  2. maven system path,加载本地jar

    当引用第三方包,且没有源代码时候,可以使用system path <dependency> <groupId>ctec</groupId> <artifact ...

  3. zabbix入门之添加监控项

    zabbix入门之添加监控项 添加一个不带参数的监控项(system.cpu.switches) 进入"配置"-->"主机"选择某主机的"监控项 ...

  4. Linux学习之旅(二)Linux文档操作

    目录操作 1. 创建目录 // 目录可以是绝对路径,也可以是相对路径 mkdir 目录名 //创建一个目录 mkdir -p 目录名1/目录名1/... //一次性创建多级目录 2. 删除目录 // ...

  5. (ACM模板)栈stack

    #include<iostream> #include<cstdio> #include<stack> using namespace std; stack< ...

  6. HTML超链接应用场景

    页面间的连接 A页到B页,最常用,用于网络导航. 如图所示: ********************************************************************* ...

  7. 插入排序->希尔排序

    /** * 插入排序 */ public class InsertSort { public static void main(String[] args){ int[] arr = {5,5,2,6 ...

  8. 【Http2.0】Http2.0

    序言 目前HTTP/2.0(简称h2)已经在广泛使用(截止2018年8月根据Alexa流行度排名的头部1千万网站中,h2占比约29%,https://w3techs.com/technologies/ ...

  9. 01 spring security入门篇

    1. 环境搭建 使用SpringBoot搭建开发环境,只需在pom.xml添加如下依赖即可. <?xml version="1.0" encoding="UTF-8 ...

  10. spring-boot和redis的缓存使用

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 pom.xml配置代码: <?xml versio ...