博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1709 The Balance( DP )
阅读量:5937 次
发布时间:2019-06-19

本文共 2504 字,大约阅读时间需要 8 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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|1typedef long long LL;typedef pair
pii;#define X first#define Y secondconst int oo = 1e9+7;const double PI = acos(-1.0);const double eps = 1e-6 ;const int N = 10500;const int mod = 1e9+7;bool dp[105][N];int n , m , a[N] , b[N] , tot ;void init() { memset( dp , false ,sizeof dp ); dp[0][0] = true ; for( int i = 1 ; i <= n ; ++i ){ for( int j = 0 ; j <= tot ; ++j ){ if( dp[i-1][j] ) dp[i][j] = dp[i-1][j]; if( dp[i-1][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 = 0 ; for( int i = 1 ; i <= n ; ++i ) cin >> a[i] , tot += a[i]; init(); int cnt = 0 ; for( int i = 1 ; i <= tot ; ++i ) if( !dp[n][i] ){ b[cnt++] = i ; } cout << cnt << endl ; for( int i = 0 ; i < cnt ; ++i ) cout << b[i] << (i+1==cnt?"\n":" ");}int main(){// freopen("in.txt","r",stdin); ios::sync_with_stdio(false); while( cin >> n ) Run();}
View Code

 

 

转载于:https://www.cnblogs.com/hlmark/p/4204620.html

你可能感兴趣的文章
winform 批量控件取值赋值
查看>>
Div+css 怎么让一个小div在另一个大div里面 垂直居中
查看>>
我要学习go语言!
查看>>
解决-修改SQL 2005 Express混合认证模式
查看>>
我的Java开发学习之旅------>银行业务调度系统
查看>>
Android eclipse 程序调试
查看>>
请听一个故事------>讲述一段失败的创业经历 ,希望你能从中受到启发
查看>>
详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作...
查看>>
js组成之dom_dom对象样式操作及运用
查看>>
jquery_jquery动态创建元素及应用
查看>>
[转载]Windows 2012 R2安装SharePoint 2013 手动安装工具软件
查看>>
Filter学习(三)Filter(过滤器)常见应用
查看>>
Algs4-2.1.20希尔排序的最好情况
查看>>
a letter and a number
查看>>
图解HTTPS
查看>>
哇哦!恍然大悟般的“share”功能的实现!
查看>>
Java MVC设计模式
查看>>
ASP.NET文件下载详细步骤
查看>>
数据分析的能力体系和进阶路线
查看>>
深度探索区块链/基于数字证书的成员管理服务(8)
查看>>