UVA 10280 Old Wine Into New Bottles(dp完全背包)
来源:互联网 发布:wt网络语言什么意思啊 编辑:程序博客网 时间:2024/05/23 22:52
Problem C: Old Wine Into New Bottles
Wine bottles are never completely filled: a small amount of air must be left in the neck to allow for thermal expansion and contraction. If too little air is left in the bottle, the wine may expand and expel the cork; if too much air is left in the bottle, the wine may spoil. Thus each bottle has a minimum and maximum capacity.Given a certain amount of wine and a selection of bottles of various sizes, determine which bottles to use so that each is filled to between its minimum and maximum capacity and so that as much wine as possible is bottled.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.The first line of input contains two integers: the amount of wine to be bottled (in litres, between 0 and 1,000,000) and the number of sizes of bottles (between 1 and 100). For each size of bottle, one line of input follows giving the minimum and maximum capacity of each bottle in millilitres. The maximum capacity is not less than 325 ml and does not exceed 4500 ml. The minimum capacity is not less than 95% and not greater than 99% of the maximum capacity. You may assume that an unlimited number of each bottle is available.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.Your output should consist of a single integer: the amount of wine, in ml, that cannot be bottled.
Sample Input
210 24450 4500725 75010000 24450 4500725 750
Sample Output
2500
题意:给定c升酒,然后有n种瓶子,每个瓶子有最少装酒min毫升和最多装酒max毫升。 并且min <= 0.99 max。要求出用这些瓶子来装酒最后剩下酒最少的升数。
思路:dp,明显是背包。但是c最大100W换算为升酒有10E。果断会超时。这时候观察题目,可以发现。
假设我们用K瓶去装酒。那么左右区间长度会越来越大,最后导致重合。所以当酒量达到一定上限之后,是肯定能完全装下去的。 推导一下:设重合时候是装了k瓶,这样有。k*max>=(k+1)*min,解得k>=min/(max-min),而当酒量x>=k*min的时候,就一定能全被装进去,这样就有x>=min*min/(max-min)。
代码:
#include <stdio.h>#include <string.h>const int INF = 2000000000;int t, c, n, i, j, dp[500000], m, v[4555], vis[4555], limit;struct P {int min;int max;} p[105];int max(int a, int b) {return a > b ? a : b;}int min(int a, int b) {return a < b ? a : b;}int main() {scanf("%d", &t);while (t --) {limit = INF;scanf("%d%d", &c, &n);c *= 1000;for (i = 0; i < n; i ++) {scanf("%d%d", &p[i].min, &p[i].max);limit = min(limit, p[i].min * p[i].min / (p[i].max - p[i].min));}if (c >= limit) {printf("0\n");if (t)printf("\n");continue;}memset(dp, 0, sizeof(dp));memset(vis, 0, sizeof(vis));m = 0;for (i = 0; i < n; i ++)for (j = p[i].min; j <= p[i].max; j ++)if (!vis[j]) {vis[j] = 1;v[m ++] = j;}for (i = 0; i < m; i ++) {for (j = v[i]; j <= c; j ++) {dp[j] = max(dp[j], dp[j - v[i]] + v[i]);}}printf("%d\n", c - dp[c]);if (t)printf("\n");}return 0;}
- UVA 10280 Old Wine Into New Bottles(dp完全背包)
- uva 10280 - Old Wine Into New Bottles(完全背包)
- UVa 10280 Old Wine Into New Bottles / 完全背包
- UVA - 10280 Old Wine Into New Bottles 剪枝+完全背包
- uva 10280 Old Wine Into New Bottles 有剪枝的完全背包
- UVA - 10280Old Wine Into New Bottles(完全背包+剪枝)
- uva 10280 Old Wine Into New Bottles (有剪枝的完全背包)
- Old Wine Into New Bottles - UVa 10280 dp
- UVA - 10280 Old Wine Into New Bottles
- uva 10280 - Old Wine Into New Bottles
- UVA 10280 - Old Wine Into New Bottles
- uva 10280 - Old Wine Into New Bottles
- UVA - 10280 Old Wine Into New Bottles
- uva10280 - Old Wine Into New Bottles
- (中等)动态规划 HOJ 1770 Old Wine Into New Bottles
- POJ 2614 Old Wine Into New Bottles 笔记
- 304. New wine in old bottles. 旧瓶装新酒
- uva 10280(完全背包)
- 数据库(DB)语句的规格化
- Delete
- WA 裸强联通判断 hdu Problem-1269 迷宫城堡
- linux关于bashrc与profile的区别(转)
- 二叉搜索树(二叉排序树)
- UVA 10280 Old Wine Into New Bottles(dp完全背包)
- 华为2014校园招聘笔试,围棋吃子判断
- 有限状态机的实现
- JQuery插件开发
- 1
- 数据库压力测试工具mysqlslap
- Java 的布局管理器GridBagLayout的使用方法【图文说明】
- 1
- 常用的图像处理小算法