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. Vhost Architecture

    在前面的文章中在介绍virtio机制中,能够看到在通常的应用中一般使用QEMU用户态程序来模拟I/O訪问,而Guest中的数据要通过Guest到Host Userspace的第一次拷贝,再经过Host ...

  2. Xamarin 弹窗

    包括通知类弹窗和选择类弹窗,以下是安卓手机的显示效果 关键代码 DisplayAlert("Alert", "You have been alerted", & ...

  3. .NET Core 3.0 Preview 5 亮点之一:发布单文件可执行程序

    在阅读 Announcing .NET Core 3.0 Preview 5 时发现了 .NET Core 3.0 Preview 5 的一个新特性 —— Publishing Single EXEs ...

  4. android studio 3.0+发布签名apk注意的情况

    在build.gradle for module文件中添加 lintOptions { checkReleaseBuilds false abortOnError false } 这样避免失败

  5. VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测(超详细截图)

    http://www.cnblogs.com/yipu/p/3611611.html http://bbs.feng.com/read-htm-tid-7625465.html OS X Maveri ...

  6. windows Service 之调试过程(附加到进程里调试,而且启动时间不能超过30秒)

    最近第一次用C#写了一个windows service ,其实实现的内容比较简单.就是启动remoting 连接,但是调试相对初次写windws service 的我来说,比较烦.没有经验,而且没办法 ...

  7. OSGI资料

    http://osgi.codeplex.com/ http://www.iopenworks.com/

  8. Bitmap的读写和几个小儿科的滤镜效果~

    闲来玩玩图像处理,拿破仑说过:“不想自己实现滤镜的美工不是好程序员~~#@!*^...#&!@......”  因为在学校做过很多美工的工作,而且从小就喜欢画画所以对图像相关的东西都还比较感兴 ...

  9. Spark之SparkSql

    -- Spark SQL 以编程方式指定模式 val sqlContext = new org.apache.spark.sql.SQLContext(sc) val employee = sc.te ...

  10. python正则表达式模块

    正则表达式是对字符串的最简约的规则的表述.python也有专门的正则表达式模块re. 正则表达式函数 释义 re.match() 从头开始匹配,匹配失败返回None,匹配成功可通过group(0)返回 ...