codeforces 730 j.bottles

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;
}
posted @ 2018-05-17 18:04  会打表的toby  阅读(397)  评论(2编辑  收藏  举报