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. upx压缩notepad.exe(运行时压缩)

    PEView:https://www.lanzous.com/i5k9vbg UPX:https://www.lanzous.com/i5k9vch notepad.exe:https://www.l ...

  2. C#编程--第一天

    C#编程 一. 了解C#: 1. C#的定义及其特点 2.vs的集成开发环境:熟悉了解vs2012 二.C#语言基础 1.C#项目的组成结构: .config----配置文件(存放配置参数文件) .c ...

  3. RabbitMQ ——简单队列

    一 .概述 我们不从开始就讲述基本的概念,尤其是在Rabbitmq之中有些概念确实比较难以理解,我们首先做的就是将光放提供的消息模型 进行实现,然后再总结一下Rabbitmq之中的基本概念. 二 .基 ...

  4. iOS crash log 解析

    iOS开发中,经常遇到App在开发及测试时不会有问题,但是装在别人的设备中会出现各种不定时的莫名的 crash,因为iOS设备会保存应用的大部分的 crash Log,所以可以通过 crash Log ...

  5. rabbitmq windows安装 及 centos安装

     windows安装如下: 安装方法如下网址: https://baijiahao.baidu.com/s?id=1605656085633071281&wfr=spider&for= ...

  6. NVIDIA Jetson™ TX1

    NVIDIA® Jetson TX1 是一台模块式计算机,代表了视觉计算领域近20年的研发成就,其尺寸仅有信用卡大小.Jetson TX1 基于崭新 NVIDIA Maxwell™ 架构,配有256个 ...

  7. lvm相关

    LVM 概念:PV(单个硬件)--VG(组合)--LV(分区) pv打头的:代表pv相关的命令vg带头的:代表vg相关的命令lv带头的: 代表lv相关的命令 create:创建相关remove:移除相 ...

  8. IBM IMM默认ID

    默认ID: http://192.168.70.125用户名:USERID密码:PASSW0RD (数字0)

  9. getString()方法与getObject()方法的区别

    JDBC提供了getString().getInt()和getData()等方法从ResultSet中获取数据,当查询结果集中的数据量较小时,不用考虑性能,使用这些方法完全可以满足需求,但是当Resu ...

  10. 浅析 http 接口

     一.HTTP接口 Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通过TCP建立起一个到服务器的连接通道,当本次请求需要的数据完毕 ...