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 ...
随机推荐
- WPF 左键单击弹出菜单 ContextMenu
原文:WPF 左键单击弹出菜单 ContextMenu WPF中的ContextMenu在XAML中可直接做出来,但是仅限于右键弹出菜单,如果需要添加左键弹出功能,只需要在事件中添加Click事件 X ...
- PHP中的序列化
接口 Serializable { abstract public string serialize (void); abstruact public void unserialize (string ...
- C# 多进程安全
多个应用程序同时写入数据到一个文件中时可用 public void WriteData(string dataWh, string filePath) { EventWaitHandle waitHa ...
- 探究操作系统的内存分配(malloc)对齐策略
问题: 我们在写程序的时候经常发现程序使用的内存往往比我们申请的多,为了优化程序的内存占用,搅尽脑汁想要优化内存占用,可是发现自己的代码也无从优化了,怎么办?现在我们把我们的焦点放到malloc上,毕 ...
- 【shell】gerrit同步备份脚本的实现
本期分享下gerrit如何实现备份,由于之前的一台代码服务器直接down掉,所以为了以防万一,开始备份gerrit数据,有人说,gerrit不过是个审查代码的工具,备份这个做什么,git是分布式代码管 ...
- android中优雅实现recyclerview
在慕课网上看了Abner_泥阿布老师不一样的RecyclerView优雅实现复杂列表布局这个视频课程,本人在Ubuntu16.04LTS下使用android studio2.2.3按照老师讲解写的代码 ...
- vmstat命令浅析
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- JSON对象与字符串之间的相互转换 - CSDN博客
原文:JSON对象与字符串之间的相互转换 - CSDN博客 <html> <head> <meta name="viewport" content=& ...
- Docker笔记03-docker 网络模式
docker网络模式分为5种 Nat (Network Address Translation) Host other container none overlay 第一种 Nat模式 docker的 ...
- 2015元旦第一弹——WP8.1应用程序栏(C#后台代码编写)
//第一次写博文,以后还请各位道友互相关照哈.废话不多说,直接进入正题. 相信大家对于如何在XAML添加应用程序栏应该很清楚,不清楚的话,可以打开新建个Pviot应用 就有系统自带的菜单栏. 本文主要 ...