kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35043 Accepted Submission(s): 17420
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!
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
The minimum amount of money in the piggy-bank is 100.
This is impossible.
题目大意:给出存钱罐的容量,和各种货币的价值的重量,然后求存钱罐里放满的最坏情况(里面钱最少)。
思路:乍一看就是完全背包,但是用贪心一发WA,然后就学习了完全背包。注意一点,这里是求最小的价值。所以初始化条件要注意一下。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head const int MAX = + ;
int w[MAX], c[MAX];
int dp[+]; //dp[i][v] 表示 前i个物品恰好放到容量为v的 最小价值 int main() {
int _, n, st, ed;
for(scanf("%d", &_);_;_--) {
scanf("%d%d", &st, &ed);
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &c[i], &w[i]);
}
int V = ed - st;//容量
for(int i = ; i <= V; i++)
dp[i] = inf;// 初始化位inf 如果dp[V]为inf 那么就是没有装满
dp[] = ;//初始化~!!
for(int i = ; i < n; i++) {//核心代码
for(int v = w[i]; v <= V; v++)
dp[v] = min(dp[v], dp[v-w[i]] + c[i]);
}
if(dp[V] != inf)
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[V]);
else
printf("This is impossible.\n");
}
}
kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)的更多相关文章
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- kuangbin专题十二 HDU1260 Tickets (dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kuangbin专题十二 HDU1087 Super Jumping! Jumping! Jumping! (LIS)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- kuangbin专题十二 HDU1069 Monkey and Banana (dp)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- does not contain bitcode. You must rebuild it with
*** does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE ...
- JDBC连接MYSQL,批量执行SQL语句或在执行一个SQL语句之前执行一个SQL语句
conn = MysqlJdbcUtils.getConnection(); Statement ps=conn.createStatement(); ps.addBatch("trunca ...
- 下拉框value ,selectedIndex
- pageBean实现分页
PageBean类 package com.xujingyang.domain ; import java.util.List ; /** * @author oldmonk * @date 2017 ...
- 在Oracle 12C中使用scott账号
在Oracle11g中默认是有scott账号的,但在Oracle 12C中则不能直接使用. 我的机器环境: 操作系统:Windows Server 2008 R2 64位 Oracle版本:Oracl ...
- php学习笔记-关联数组
传统的数组定义方法如下: <?php $names[0]= 'chinese'; $names[1]= 'math'; $names[2]= 'english'; echo $names[2]; ...
- 算法Sedgewick第四版-第1章基础-020一按优先级计算表达式的值
/****************************************************************************** * Compilation: javac ...
- Mat类的输出格式
从前面的例程中, 可以看到 Mat 类重载了<<操作符, 可以方便得使用流操作来输出矩阵的内容.默认情况下输出的格式是类似 Matlab 中矩阵的输出格式.除了默认格式,Mat 也支持其他 ...
- 数据结构_calculator
问题描述 小 V 发明了一个神奇的整数计算器:给定一个合法的表达式,这个计算器能求出这个表达式的最终答案.表达式可能包含:+:运算符,整数加法.如 1+1=2-:运算符,整数减法.如 1-1=0*:运 ...
- 手把手教Android商业项目-即时通讯-i美聊
[课程概况] 手把手教你从无到有的完整实现一个Android商业项目,是目前整个市场上所没有的课程,废话不多说,请往下看. [项目概况] 项目名称:i美聊 所属领域:移动社交 即时通讯 代码行数: ...