Codeforces 730J:Bottles(背包dp)
http://codeforces.com/problemset/problem/730/J
题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至少需要几个瓶子,还有最少需要倒的水量(把一个瓶子的水倒到另一个瓶子的总水量)。
思路:是一个背包dp,dp[i][j] 表示选择i个瓶子容量为j的时候当前拥有的最大的水量。不过第三层循环当时不知道应该怎么写。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 105
#define INF 0x3f3f3f3f
struct node {
int a, b;
} p[N];
int dp[N][N*N]; // dp[i][j] 表示选择i个瓶子容量为j的时候当前拥有的最大的水量
bool cmp(const node &a, const node &b) { return a.b > b.b; }
int main()
{
int n, sum = , tmp, cnt = , tol = , ans = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &p[i].a), sum += p[i].a;
for(int i = ; i <= n; i++) scanf("%d", &p[i].b), tol += p[i].b;
tmp = sum;
sort(p + , p + + n, cmp);
for(int i = ; i <= n; i++) {
tmp -= p[i].b;
if(tmp <= ) { cnt = i; break; } // 确定最终的瓶子数
}
memset(dp, -, sizeof(dp));
dp[][] = ;
for(int i = ; i <= n; i++) {
for(int j = tol; j >= p[i].b; j--) { // 类似01背包
for(int k = i; k > ; k--) { // 之前选择的状态再加上目前的第i个瓶子能否得到优化,这样就可以枚举所有情况了
if(dp[k-][j-p[i].b] != -) { // 如果上一个状态有解
dp[k][j] = max(dp[k][j], dp[k-][j-p[i].b] + p[i].a);
}
}
}
}
for(int i = sum; i <= tol; i++)
ans = max(ans, dp[cnt][i]);
printf("%d %d\n", cnt, sum - ans);
return ;
}
Codeforces 730J:Bottles(背包dp)的更多相关文章
- Codeforces 946 课程表背包DP 数位DFS构造
A B 给你A,B 两个数 1.a=0 OR b=0 break 2.a>=2b a=a-2b 3.b>=2a b=b-2a 如果只是单纯模拟肯定会超时 ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- Codeforces 864E Fire(背包DP)
背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp
D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...
- Codeforces 922 E Birds (背包dp)被define坑了的一题
网页链接:点击打开链接 Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp ne ...
- 背包dp整理
01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
随机推荐
- Python 实现 淘宝秒杀 聚划算 自己主动提醒 源代码
说明 本实施例可以监视一起购买的成本button,当警报济济一堂花费时间整点到达(音频文件自定义位置)而自己主动跳出页面(URL习惯). 同一时候还能够通过命令行參数自己定义刷新间隔时间(默认0.1s ...
- Jenkins build失败条件
在Jenkins 项目写了很多剧本.有时候,我发现脚本失败,但Jenkins运行成功. Jenkins无论是通过退出代码0比量build成功. 因此,newLISP在.您可以使用(exit)对于成功. ...
- SQL之Grant(分配权限)和Revoke(回收权限)
Grant Grant可以把指定的权限分配给特定的用户,如果这个用户不存在,则会创建一个用户 命令格式 grant 权限 on 数据库名.表名 to 用户名@登陆方式 identified by 'p ...
- gnuradio companion 找不到第三方模块gr-osmosdr的问题
我使用了来自Ettus的gnuradio软件包,之后安装了gr-osmosdr 以在gnuradio中调用RTL电视棒. 但是在gnuradio companion找不到来自rtlsdr-source ...
- 【C#】wpf查找父子节点
原文:[C#]wpf查找父子节点 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- 原 BinaryWriter和BinaryReader(二进制文件的读写)
原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 ...
- tensorflow 1.0 学习:模型的保存与恢复
将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf. ...
- 你遗忘的都在这里—iOS常用类型方法笔记
这些都是项目中常用但又常忘的方法,与大家分享一下. 一.NSString 创建字符串. NSString *astring = @"This is a String!"; 创建空 ...
- 零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」
原文:零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」 本章将介绍UseLayoutRounding ...
- Creating a Linux Daemon (service) in Delphi
With the introduction of the Linux target for Delphi, a wide range of possibilities are opened up to ...