题目大意

有N个数字,大小为a[i], 给定一个数S,用这N个数中的某些数加起来使得结果sum>= S,且sum-S最小,求该最小的sum-S值。

题目分析

题意中可知,这N个数字的和肯定大于S。那么可以先判断对于大于S的数s1,能否利用这N个物品的某些组合得到,然后找到最小的s1即可。利用动态规划的思想,设f[i][w] 表示能否利用前i种物品的某个组合加和得到w,有f[i][w] = f[i-1][w] || f[i-1][w-a[i]] + a[i]. 
    那么问题来了,要求的f[i][w]的w的范围是多少呢?设最大的a[i]为A,首先w肯定要大于S,然后w小于S+A。假设w大于等于S+A,那么即使知道了某个 f[i][w]为true(即可以通过N个数的某个组合加和得到w),那么肯定可以通过这N个数字的加和得到 w - a[i] >= S(a[i]为构成w的数字的集合中的任意一个数字)。在选择s1的时候会选择w-a[i]而非w,从而w大于等于S+A没有意义。

实现(c++)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define MAX_HEIGHT 20000005
int cow_height[25];
bool f[MAX_HEIGHT];
int max(int a, int b){
return a > b ? a : b;
}
int main(){
int n, b_height, total_height = 0, max_height = 0;
while (scanf("%d %d", &n, &b_height) != EOF){
total_height = 0, max_height = 0;
for (int i = 0; i < n; i++){
scanf("%d", cow_height + i);
total_height += cow_height[i];
max_height = max(max_height, cow_height[i]);
}
int m = max_height + b_height;
for (int i = 0; i <= m; i++){
f[i] = false;
}
f[0] = true;
f[cow_height[0]] = true;
for (int i = 1; i < n; i++){
for (int w = m; w >= cow_height[i]; w--){
f[w] = f[w] || f[w - cow_height[i]];
}
}
for (int i = b_height; i < m; i++){
if (f[i]){
printf("%d\n", i - b_height);
break;
}
}
}
return 0;
}

poj_3628 动态规划的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. EMQ ---客户端上线自动订阅主题

    通过修改配置文件即可实现. emq v2.3.11,软件架构做了调整,把功能集成在了emq_modules模块,/data/loaded_plugins默认会加载emq_modules. 我们只需要改 ...

  2. python pivot() 函数

    以下为python pandas 库的dataframe pivot()函数的官方文档: Reshape data (produce a “pivot” table) based on column ...

  3. swift 单例模式

    class ca{ ; static var instance:ca = ca(); class func GetInstance()->ca{ return instance; } } var ...

  4. 动态修改 dom 元素的伪类样式

    最近写代码,需要修改伪类的 content 属性,不想定义两个样式进行切换,而是直接通过 js 进行修改. html 中的伪类(如 a:hover / a:link / class::before / ...

  5. iptables安装失败后-------------firewalld回归

    yum install firewalld systemctl stop iptables; systemctl mask iptables; systemctl unmask firewalld s ...

  6. Mac下Selenium无法最大化Chrome解决方案

    在用Selenium做自动化操作时,一般最大化浏览器的代码都是:driver.manage().window().maximize(), 但是在Mac下这样是无法最大化Chrome浏览器的,解决方法: ...

  7. 开源搜索引擎评估:lucene sphinx elasticsearch (zhuan)

    http://lutaf.com/158.htm ************************ 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch s ...

  8. 折腾kali linux2.0

    偶然的机会了解到了kali linux这个用于渗透测试的linux发行版,于是就从官网下了iso,但是制作启动盘老出错.网上查了下说在linux下用dd命令特别简单,于是转到ubuntu下制作启动盘, ...

  9. js中获取event keycode的兼容办法

    window.onkeypress=function(e){ var event = e || window.event, //在ff下event会做为参数传进来,ie下会在window下 keyCo ...

  10. Hadoop环境搭载

    官网安装教程:[http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.13.0/hadoop-project-dist/hadoop-com ...