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 ...
随机推荐
- Java设计模式(15)——行为模式之策略模式(Strategy)
一.概述 概念 UML简图 角色 二.实践 我们先将上述的UML图的抽象情况下的代码写出,然后再给出一个具体的例子 策略接口——当然如果有一些公共的行为,应当使用抽象类! /** * 策略接口 * * ...
- BZOJ1968_COMMON约数研究_KEY
题目传送门 BZOJ水题,for i=1~N,答案加上N/i即可 ANS=∑N/i(i∈{1~N}) code: /****************************************** ...
- Android:Gradle报错——No resource found that matches the given name (at 'dialogCornerRadius' with value '?android:attr/dialogCornerRadius')
今天在使用科大讯飞语音识别SDK进行语音识别功能实现时,莫名的引入了这个错误.不得不吐槽Android Studio再引入别的包时太容易出现冲突,然后导致无法找到R文件,项目无法执行. 1. 具体报错 ...
- Python3 之选课系统
项目介绍:项目名称:(oldboy选课系统)项目功能: 分为 学员视角, 老师视角 , 管理员视角 学员视角{ (注册 登录 个人中心 选课 学习 上课) 登录 就是登录 注册: 填写 资料 信息 完 ...
- 「日常训练」Duff in the Army (Codeforces Round #326 Div.2 E)
题意(CodeForces 588E) 给定一棵\(n\)个点的树,给定\(m\)个人(\(m\le n\))在哪个点上的信息,每个点可以有任意个人:然后给\(q\)个询问,每次问\(u\)到\(v\ ...
- Linux命令应用大词典-第36章 密码和证书管理
36.1 pwdhash:密码哈希生成器 36.2 mkpasswd:生成应用于用户的新密码 36.3 keytool:密钥和证书管理工具 36.4 certutil:证书服务器管理工具 36.5 v ...
- Python字符串操作大全(非常全!!!)
1. python编程里字符串的内置方法(非常全) capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居 ...
- ObjectMapper的使用
Jackson ObjectMapper类 ObjectMapper类是Jackson库的主要类它提供一些功能将Java对象转换成JSON结构,反之亦然它使用JsonParser和JsonGenera ...
- pthon web框架flask(二)--快速入门
快速入门 迫切希望上手?本文提供了一个很好的 Flask 介绍.假设你已经安装 Flask, 如果还没有安装话,请浏览下 安装 . 一个最小的应用 一个最小的应用看起来像这样: from flask ...
- [Clr via C#读书笔记]Cp16数组
Cp16数组 一维数组,多维数组,交错数组:引用类型:P338的图非常的清楚地描述了值类型和引用类型在托管堆中的关系:越界检查: 数组初始化 数组初始化器: 四种写法 string[] names = ...