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. USTC现代软件工程--summary

    起笔:我希望先简单总结一下我在这门课程中经历的一些工作以及学习到的一些东西,再对自己.队友.老师做一个评价.然后我想提出一些对这门课程的一些看法和建议,与自己的心得体会. 第一部分: 我在这门课上经历 ...

  2. 关于WTSAPI32

    一般在windows编程都是用用从ntdll导出的Native API,现在看到一点COM编程或者其他的一些不常用的接口函数总觉得蛮有意思,准备以后多积累一下. 先简单总结WTSAPI32.以下实在W ...

  3. windows java jdk安装

    安装 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 勾选协议,选择 ...

  4. spring boot与ElasticSearch的集成

    本文主要介绍Spring boot与ElasticSearch的集成,因为Spring boot的教程以及ElasticSearch的学习其他博客可能更优秀,所以建议再看这篇文章前先学习学习一下Spr ...

  5. wxpython总体

    人到夏天就特别懒 from math import * import wx def sin_fun(event): a=text_angle.GetValue() b=sin(radians(floa ...

  6. 基于dokcer的zoookeeper集群安装

    编写docker-compose.ymlversion: '3.1' services: zoo1: image: zookeeper restart: always hostname: zoo1 p ...

  7. JDBC调用oracle 存储过程

    1.创建一个oracle存储过程 p_empInfo2 并执行,使这段sql代码能编译存储到oracle数据库中. --输入员工号查询某个员工(7839)信息,将薪水作为返回值输出,给调用的程序使用 ...

  8. SpringMvc中乱码问题的解决

    一:如果是前台传递的数据有问题. 在tomcat的service.xml中加上: URIEncoding="UTF-8" <Connector URIEncoding=&qu ...

  9. springmvc REST风格的URL

    1:需要配置一个filter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELET ...

  10. LOJ 2304 「NOI2017」泳池——思路+DP+常系数线性齐次递推

    题目:https://loj.ac/problem/2304 看了各种题解…… \( dp[i][j] \) 表示有 i 列.第 j 行及以下默认合法,第 j+1 行至少有一个非法格子的概率,满足最大 ...