杭电 1114 Piggy-Bank【完全背包】
解题思路,首先很容易想到方程f[v]=min(f[v],f[v-w[i]+p[i]),因为是要求当包装满的时候(因为题目中给出的是包的质量是一定的),包里面装的钱最少,所以将f[]初始化成一个很大的数。
然后对于这个循环
for(i=1;i<=n;i++)
{
for(v=w[i];v<=m;v++)
f[v]=min(f[v],f[v-w[i]+p[i]);//可以理解为只要不超过包的容量,你可以任意放入该种硬币。
}
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
#include<stdio.h>
#define max 1000000000
int p[50005],w[10010],f[10010];
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int ncase,i,v,m,e,r,n;
while(scanf("%d",&ncase)!=EOF)
{
while(ncase--)
{
for(i=1;i<10010;i++)
f[i]=max;
scanf("%d %d",&e,&r);
m=r-e;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&p[i],&w[i]); for(i=1;i<=n;i++)
{
for(v=w[i];v<=m;v++)
f[v]=min(f[v],f[v-w[i]]+p[i]);
}
if(f[m]<max)
printf("The minimum amount of money in the piggy-bank is %d.\n",f[m]);
else
printf("This is impossible.\n"); } }
}
杭电 1114 Piggy-Bank【完全背包】的更多相关文章
- 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...
- Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 杭电 2159 fate(二维背包费用问题)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 杭电 1114 Piggy-Bank 完全背包问题
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 杭电dp题集,附链接还有解题报告!!!!!
Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱 最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...
- 杭电ACM题单
杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...
- 杭电acm习题分类
专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
随机推荐
- 应用二:Vue之ElementUI Form表单校验
(注:本文适用于有一定Vue基础或开发经验的读者,文章就知识点的讲解不一定全面,但却是开发过程中很实用的) 表单校验是前端开发过程中最常用到的功能之一,根据个人的工作经验总结在此对表单校验功能的基 ...
- ubuntu区域语言(locale)设置(切换为中文)
第一步:编辑文件 代码:sudo gedit /var/lib/locales/supported.d/local (这个文件是所有已经激活的区域语言的列表) 写入相关内容,比如写入如下内容: zh ...
- MySQL 单表查询多表查询
一 单表查询 表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...
- 4056 hdu4866 Shooting
题目描述 In the shooting game, the player can choose to stand in the position of [1, X] to shoot, you ca ...
- HDU-3015 Disharmony Trees [数状数组]
Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...
- Java包名称中通配符的含义
"com.abc 表示的意义为:系统从com.abc这个包及其子孙包扫描组件 "com.abc.* 表示的意义为:系统从com.abc这个包的子孙包扫描组件
- mplayer configure选项中文注释
mplayer configure选项中文注释(MPlayer-1.0rc2) http://blogold.chinaunix.net/u3/104581/showart_2322466.html ...
- using_where, Using temporary ,using_filesort 分享
Using filsort文档中的解释: Mysql需要额外的一次传递,以找出如何按排序顺序检索行,通过根据联接类型浏览所有行并为所有匹配where子句的行保存排序关键字和行的指针来完成排序,然后关键 ...
- SQLPlus在连接时通常有四种方式
SQLPlus在连接时通常有四种方式 1. ? 1 sqlplus / as sysdba 操作系统认证,不需要数据库服务器启动listener,也不需要数据库服务器处于可用状态.比如我们想要启动数据 ...
- C#中的文本乱码问题
文本乱码问题 //提供一种解决C#文本乱码的解决思路 //写入使用: string str; str = this.menu.Text; string fname = Application.Star ...