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 ...
随机推荐
- BZOJ1879_Bill的挑战_KEY
题目传送门 第一次看题目感觉毫无还手之力,一看M的范围≤15,果断状压. 但是状压的想法比较新奇. 先想到的状压是设f[i][j]表示前i个状态为j时的方案总数,但是后来想了一想不行,会超时. 于是以 ...
- 天津市人民优步Uber司机奖励政策(9月14日~9月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- WEB安全--高级sql注入,爆错注入,布尔盲注,时间盲注
1.爆错注入 什么情况想能使用报错注入------------页面返回连接错误信息 常用函数 updatexml()if...floorextractvalue updatexml(,concat() ...
- 跟浩哥学自动化测试Selenium -- 浏览器的基本操作与元素定位(3)
浏览器的基本操作与元素定位 通过上一章学习,我们已经学会了如何设置驱动路径,如何创建浏览器对象,如何打开一个网站,接下来我们要进行一些复杂的操作比如先打开百度首页,在打开博客园,网页后退,前进等等,甚 ...
- Python列表的深拷贝和浅拷贝
1. Python列表的拷贝 对于python里面如果想要进行列表的拷贝和复制,具体的操作语句如下: 1) 深拷贝: M=[A,b,a,c] N=M[:] 2) 浅拷贝: N=M 有人说可以直接将M赋 ...
- python 中的reload(sys)
import sys reload(sys) sys.setdefaultencoding('utf-8') #python2中的使用方法 #重新载入 sys 模块,并设置默认编码为 utf8 & ...
- Linux文件系统简介和软链接和硬链接的区别
Linux有着极其丰富的文件系统,大体可分为如下几类: 网络文件系统:如nfs.cifs等: 磁盘文件系统:如ext3.ext4等: 特殊文件系统:如prco.sysfs.ramfs.tmpfs等: ...
- SSH:远程登陆
SSH用于计算机之间的加密登录的前提是公钥为真,所以存在中间人攻击中间人攻击:与https协议不同,SSH协议的公钥是没有CA公证的,当对公钥的请求被中间截获时,中间人可以发出伪造公钥干坏事而不被识破 ...
- @meida 媒体查询
示例 @meida 媒体查询 在进行书写的时候需要考虑到加载顺序和样式权重使用meida响应式实现不同宽度布局示例 常用工具 https://mydevice.io 参考链接 https://deve ...
- DeepLearning - Forard & Backward Propogation
In the previous post I go through basic 1-layer Neural Network with sigmoid activation function, inc ...