J. Bottles
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
Input
4
3 3 4 3
4 7 6 5
Output
2 6
Input
2
1 1
100 100
Output
1 1
Input
5
10 30 5 6 24
10 41 7 8 24
Output
3 11
Note

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

  1. Codeforces 730 J.Bottles (01背包)

    <题目链接> 题目大意: 有n个瓶子,各有水量和容量.现在要将这写瓶子里的水存入最少的瓶子里.问你最少需要的瓶子数?在保证瓶子数最少的情况下,要求转移的水量最少. 解题分析:首先,最少的瓶 ...

  2. Codeforces 730J:Bottles(背包dp)

    http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...

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

  4. J. Bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  5. J. Bottles 二维费用背包问题

    http://codeforces.com/contest/730/problem/J 3 4    36    1 90   45   40 其实可以知道,选出多少个瓶子呢?是确定的,当然选一些大的 ...

  6. 【模拟】Codeforces 671A Recycling Bottles

    题目链接: http://codeforces.com/problemset/problem/671/A 题目大意: A和B在一张二维平面上,平面上有N个垃圾,垃圾桶只有一个在T,问把所有垃圾全扔进垃 ...

  7. CodeForces 671A Recycling Bottles

    暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...

  8. Codeforces 671A Recycling Bottles(贪心+思维)

    题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...

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

随机推荐

  1. Caliburn.Micro 杰的入门教程5,Window Manager 窗口管理器

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...

  2. 北京Uber优步司机奖励政策(1月25日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 成都Uber优步司机奖励政策(4月2、3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. DSP28335的上手试用LED灯闪烁-第一篇

    1. 本次以三兄弟的DSP28335开发板为例,看下JTAG接口,EMU0,EMU1的用途,不是很懂,不深入研究,用到再说 EMU0/1是TI芯片的JTAG才有的信号,本身不属于JTAG标准里的信号, ...

  5. Ruby 基础教程1-3

    1.命令行参数ARGV[] 2.文件读取 file=File.open(filename)    text=file.read  print text file.close 一次读取所有内容耗内存,耗 ...

  6. 微信小程序—day03

    昨日问题 接着上一篇,昨天遇到的scroll-view组件不能滚动的问题. 今天经过调试,发现是由于:图片的实际宽高,大于给image设定的宽高导致的. 解决办法: 减小图片的实际宽高,使之小于ima ...

  7. Kotlin对象:仅一行代码就可创建安全的单例

    作者:Antonio Leiva 时间:Jun 20, 2017 原文链接:https://antonioleiva.com/objects-kotlin/ Kotlin对象是Android开发人员不 ...

  8. SpringMVC+mybatis+maven+Ehcache缓存实现

    所谓缓存,就是将程序或系统经常要调用的对象存在内存中,以便其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文 ...

  9. 使用InstallShield-Limited-Edition制作安装包

    1.打开此网站,进行注册,获取序列码以及下载InstallShield-Limited-Edition 2.安装完成之后,打开VisualStudio,新建项目 3.填写基本应用信息 4.配置相关信息 ...

  10. 【CSV数据文件】

    文件参数化设置方法