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个杯子 给出n个杯子的初始水的体积以及杯子的体积 现在倒水 使得用最少个数的杯子装所有的水 并且在倒水的过程中 使得倒出的水尽量的少
题解:爆搜TLE 背包处理 orzzz 太菜了
dp[i][j]表示用i个杯子容量为j最多能装多少水 注意这个j的范围为杯子的体积和 (可能所选的i个杯子 就算装有全部的水 也不会装满)
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int n;
struct node
{
int a,b;
}N[];
int dp[][];
bool cmp(struct node aa,struct node bb)
{
if(aa.b>bb.b)
return true;
if(aa.b==bb.b)
{
if(aa.a>bb.a)
return true;
}
return false;
}
int main()
{
scanf("%d",&n);
int m=;
int mm;
int mm2=;
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].a);
m+=N[i].a;
}
mm=m;
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].b);
mm2+=N[i].b;
}
sort(N+,N++n,cmp);
int num=;
while()
{
m-=N[++num].b;
if(m<=)
break;
}
memset(dp,,sizeof(dp));
for(int i=;i<=num;i++)
for(int j=;j<=mm2;j++)
dp[i][j]=-INF;
dp[][]=;
for(int j=;j<=n;j++)
{
for(int k=mm2;k>=N[j].b;k--)
{
for(int l=;l<=num;l++){
dp[l][k]=max(dp[l][k],dp[l-][k-N[j].b]+N[j].a);
}
}
}
int ans=;
for(int i=mm2;i>=mm;i--)
if(dp[num][i])
ans=max(ans,dp[num][i]);
printf("%d %d\n",num,mm-ans);
return ;
}
/*
10
18 42 5 1 26 8 40 34 8 29
18 71 21 67 38 13 99 37 47 76
3 100
*/