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)的更多相关文章

  1. 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 如果只是单纯模拟肯定会超时 ...

  2. 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/ ...

  3. Codeforces 864E Fire(背包DP)

    背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...

  4. 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 \( ...

  5. Codeforces 922 E Birds (背包dp)被define坑了的一题

    网页链接:点击打开链接 Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp ne ...

  6. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  7. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  8. 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 ...

  9. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

随机推荐

  1. Oracle召回

    后几集录制视频,记录在记录开始的时候不知道怎么,录了几集没有什么,够又一次录,过程中也也把Oracle数据库这部分看了一遍,收获也挺多的,学习是反复积累的过程,对于一些零散的知识点又回想了一下,又一次 ...

  2. WPF元素绑定

    原文:WPF元素绑定 数据绑定简介:数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性.目标属性是依赖项属性.源对象可以是任何内容,从另一个WPF元素乃至ADO. ...

  3. js 动态生成button 并设置click事件

    <div id="MyDiv"></div> <script> function AddButton() { var MyDiv =docume ...

  4. 待修 Bug

    # 乱码 ## 描述 环境:Tomcat 8 + Spring 4 + Spring Security. 问题描述: 在类 AbstractAnnotationConfigDispatcherServ ...

  5. python3 无法使用flask.ext.* 报错的解决方法

    python3 使用flask的一些扩展功能的导入方法是 from flask_bootstrap import Bootstrapfrom flask_moment import Momentfro ...

  6. win32Helper

    点击别的winform窗口的按钮 #region 点击别的窗口的按钮 [DllImport("user32.dll", EntryPoint = "FindWindowA ...

  7. TP5.0中使用trace调试

    1.在项目 的配置文件config.php 配置, 2.在程序中使用trace: 3.在浏览器网页上打开 得到如下图所示:点击 “用户变量”,即可查看使用trace输出的变量 或者我们使用 trace ...

  8. iBatisNet 中 iterate标签的使用

    <iterate>标签,顾名思义是用来遍历标签用的. 支持的属性如下: 属性 说明 是否必选 open 遍历后生成的这些sql,开始的第一个符号 可选 close 遍历后生成的这些sql, ...

  9. ext中设置grid单选,取消选择

    原代码(无法取消选择)如下: selModel: Ext.create('Ext.selection.CheckboxModel', { mode: "SINGLE", allow ...

  10. [转] Protobuf高效结构化数据存储格式

    从公司的项目源码中看到了这个东西,觉得挺好用的,写篇博客做下小总结.下面的操作以C++为编程语言,protoc的版本为libprotoc 3.2.0. 一.Protobuf? 1. 是什么?  Goo ...