codeforces 730 j.bottles
2 seconds
512 megabytes
standard input
standard output
Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda ai and bottle volume bi (ai ≤ bi).
Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends x seconds to pour x units of soda from one bottle to another.
Nick asks you to help him to determine k — the minimal number of bottles to store all remaining soda and t — the minimal time to pour soda into k bottles. A bottle can't store more soda than its volume. All remaining soda should be saved.
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of bottles.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the amount of soda remaining in the i-th bottle.
The third line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ 100), where bi is the volume of the i-th bottle.
It is guaranteed that ai ≤ bi for any i.
The only line should contain two integers k and t, where k is the minimal number of bottles that can store all the soda and t is the minimal time to pour the soda into k bottles.
4
3 3 4 3
4 7 6 5
2 6
2
1 1
100 100
1 1
5
10 30 5 6 24
10 41 7 8 24
3 11
In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3 + 3 = 6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1 + 2 = 3 seconds. So, all the soda will be in two bottles and he will spend 3 + 3 = 6 seconds to do it.
【题意】
给$$$n$$$个瓶子的容量和含量,选出最少的瓶子,使得它们的容量和不小于总含量,且它们的含量和最大。
【分析】
因为同时要考虑两个最优,贪心是行不通的,只能每个瓶子选或者不选都试一下。首先分析一下复杂度:在最坏的情况下,看似有$$$2^{100}$$$种状态要考虑,但其实状态与状态之间是有重复的。假设总共选了$$$i$$$个瓶子,容量的组合一共有$$$c^i_{100}$$$种,但事实上,由于每个瓶子的容量是1~100,容量和的大小一共只有100*100种,所以把组合的数量,压缩到数值的数量,这就是能用dp来做的关键。
【思路】
最开始的时候,dp[0][0]=0,其他的状态都记为-1表示不可达,然后按顺序加入瓶子,拓展可到达的状态。 然后加入第一个瓶子。(0,0)$$$to$$$(1,v[0]); 然后加入第二个瓶子。(0,0)$$$to$$$(1,v[1]), (1, v[0])$$$to$$$(2,v[0]+v[1]); ...依次类推 处理完所有瓶子后,选出容量和不小于总含量,且它们的含量和最大的方案就行了。 在添加瓶子的过程中需要注意的是,可能拓展到的“新状态”之前已经被拓展过,只需要保留两者最优的就行了,也就是说对较差的状态就不继续拓展了,反正最后也一定不是最优的。
【优化】
其实需要多少个瓶子是确定的,可以通过贪心选容量最大的瓶子来获得,这样更多瓶子数的方案就不用考虑了。
【代码】
#include<stdio.h>
#include<algorithm>
#include<memory.h>
using std::sort;
#include<vector>
using std::vector; #define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mabs(x) ((x)>0?(x):(0-(x)))
#define N_max 102 inline int cmp(int a,int b) {
return a > b;
} int n, an, totc = 0,totv=0;
vector<int>v,c;
int d[N_max][N_max*N_max];//几个物品,总体积 int findn(vector<int> a) {
sort(a.begin(), a.end(), cmp);
int i, amt;
for (i = 0,amt=0;amt<totc; ++i) {
amt += a[i];
}
return i;
} int main() {
scanf("%d", &n);
v.resize(n);
c.resize(n);
for (int i = 0; i < n; ++i) { scanf("%d", &c[i]); totc += c[i]; }
for (int i = 0; i < n; ++i) { scanf("%d", &v[i]); totv += v[i]; }
an = findn(v);//利用an来剪枝
memset(d, -1, sizeof(d)); d[0][0] = 0;
/*
处理第i个物品时,给所有的已知点加上两个向量(1,v[i])和(0,0) 即选与不选
*/
int posv=0;
for (int i = 0; i < n; ++i) {//n次循环
//可能已知的范围
for (int t = an-1; t >=0; --t) {//n次循环
for (int k = posv; k >=0; --k) {//10000次循环
if (d[t][k] >= 0) {
//可能从其他的(t,k)到过(t+1, k+v[i])了,只保留最优的就行
d[t + 1][k + v[i]] =max( d[t][k] + c[i],d[t+1][k+v[i]]);
// printf("(%d,%3d) %2d->(%d,%3d) %2d\n", t, k,d[t][k], t + 1, k + v[i],d[t+1][k+v[i]]);
}
}
}
posv += v[i];//可能的总体积扩大
}
int ans = 0;
for (int i = totc; i <= totv; ++i) {
ans = max(ans, d[an][i]);
}
printf("%d %d", an, totc - ans);
return 0;
}
codeforces 730 j.bottles的更多相关文章
- Codeforces 730 J.Bottles (01背包)
<题目链接> 题目大意: 有n个瓶子,各有水量和容量.现在要将这写瓶子里的水存入最少的瓶子里.问你最少需要的瓶子数?在保证瓶子数最少的情况下,要求转移的水量最少. 解题分析:首先,最少的瓶 ...
- Codeforces 730J:Bottles(背包dp)
http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J. Bottles
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- J. Bottles
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- J. Bottles 二维费用背包问题
http://codeforces.com/contest/730/problem/J 3 4 36 1 90 45 40 其实可以知道,选出多少个瓶子呢?是确定的,当然选一些大的 ...
- 【模拟】Codeforces 671A Recycling Bottles
题目链接: http://codeforces.com/problemset/problem/671/A 题目大意: A和B在一张二维平面上,平面上有N个垃圾,垃圾桶只有一个在T,问把所有垃圾全扔进垃 ...
- CodeForces 671A Recycling Bottles
暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...
- Codeforces 671A Recycling Bottles(贪心+思维)
题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...
- Codeforces gym102058 J. Rising Sun-简单的计算几何+二分 (2018-2019 XIX Open Cup, Grand Prix of Korea (Division 2))
J. Rising Sun time limit per test 1.0 s memory limit per test 1024 MB input standard input output st ...
随机推荐
- 西安Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- springboot之RMI的使用
1.RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的 ...
- netty之粘包分包的处理
1.netty在进行字节数组传输的时候,会出现粘包和分包的情况.当个数据还好,如果数据量很大.并且不间断的发送给服务器,这个时候就会出现粘包和分包的情况. 2.简单来说:channelBuffer在接 ...
- MySQL高级-全局查询日志
注意:全局查询日志不要在生成环境中启用 一.配置启用 二.编码启用
- Http接口系列:如何提高Http接口用例的数据稳定性
此文已由作者王婷英授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 为了尽可能多的释放手工测试,提高测试效率,我们都会想到使用自动化测试,如http接口自动化测试.doubbo ...
- 一些窍门 drawable
java.lang.Object android.graphics.drawable.DrawableKnown Direct Subclasses BitmapDrawable, C ...
- 不老的神器--namp,awvs
要会使用的工具 NESSUS nmap awvs hydra burpsuit 工具的话,都有文档,应该多使用 -h 多看官方文档,就会用了. 1.namp基本用法 -iL <inputfile ...
- 怎样安装Android Studio
在浏览器地址栏输入 http://www.android-studio.org/ 打开Android Studio中文社区, 下载安装包: 这里需要注意的是SDK的目录, 我没有选择默认的目录, 而是 ...
- JVM之G1收集器
Garbage-First,面向服务端的垃圾收集器. 并行与并发:充分利用多核环境减少停顿时间, 分代收集:不需要配合其它收集器 空间整合:整体上看属于标记整理算法,局部(region之间)数据复制算 ...
- 165. Merge Two Sorted Lists【LintCode by java】
Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...