Codeforces Round #691 (Div. 1) B. Glass Half Spilled 背包DP
B. Glass Half Spilled
There are ?? glasses on the table numbered 1,…,??. The glass ?? can hold up to ???? units of water, and currently contains ???? units of water.
You would like to choose ?? glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass ?? currently contains ???? units of water, and a glass ?? contains ???? units of water. Suppose you try to transfer ?? units from glass ?? to glass ?? (naturally, ?? can not exceed ????). Then, ??/2 units is spilled on the floor. After the transfer is done, the glass ?? will contain ??????? units, and the glass ?? will contain min(????,????+??/2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass ?? to which glass ?? to pour, and also the amount ?? transferred can be any positive real number.
For each ??=1,…,??, determine the largest possible total amount of water that can be collected in arbitrarily chosen ?? glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer ?? (1≤??≤100) — the number of glasses.
The following ?? lines describe the glasses. The ??-th of these lines contains two integers ???? and ???? (0≤????≤????≤100, ????>0) — capacity, and water amount currently contained for the glass ??, respectively.
Output
Print ?? real numbers — the largest amount of water that can be collected in 1,…,?? glasses respectively. Your answer will be accepted if each number is within 10?9 absolute or relative tolerance of the precise answer.
Example
input
3
6 5
6 5
10 2
output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
for ??=1, transfer water from the first two glasses to the third one, spilling (5+5)/2=5 units and securing 2+(5+5)/2=7 units;
for ??=2, transfer water from the third glass to any of the first two, spilling 2/2=1 unit and securing 5+5+2/2=11 units;
for ??=3, do nothing. All 5+5+2=12 units are secured.
題意
現在有n個水杯,每個水杯最多盛a[i]升水,現在每個水杯已經有b[i]升水
你可以操作若干次,將A杯的水倒若干升到B杯,但是注意,你倒水的時候會灑掉一半。
請問在操作若干次之后,讓你選擇k杯,最后最多能有多少升水。
題解
我們假設已經知道了最后選擇的K杯是哪幾杯。
那么我們令最初的所有杯子的水的和為S1,最后選擇的K杯的水的和為S2,最后k杯水的容量為S3,那么:
別的杯子最多給這k杯倒 (S1-S2)/2
那么答案就是 min(S3, S2 + (S1-S2)/2) = min(S3,(S2+S1)/2)
其中的未知數有S2,S3;現在的問題就是需要找到S2和S3的關系。
DP[i][j][k],表示我們考慮前i個杯子,當前選了j個杯子,容量為k時,我最多能放多少水。
經典老DP問題了:
現在有一個背包,容積為k,我只能拿j個物品,每個物品只能拿一次,問你最大價值是多少。
代碼
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+7;
int dp[105][maxn]; // 前i個我選擇j個,容量為k的時候,最多能裝多少
int a[105],b[105],n,suma,sumb;
int main() {
cin>>n;
for (int i=1;i<=n;i++) {
cin>>a[i]>>b[i];
suma+=a[i];
sumb+=b[i];
}
for (int i=0;i<=n;i++) {
for (int j=0;j<=10000;j++) {
dp[i][j]=-1e9;
}
}
dp[0][0]=0;
for (int i=1;i<=n;i++) {
for (int j=n;j>=1;j--) {
for (int c=suma;c>=a[i];c--) {
dp[j][c]=max(dp[j][c],dp[j-1][c-a[i]]+b[i]);
}
}
}
for (int j=1;j<=n;j++) {
double ans = 0.0;
for (int c=0;c<=suma;c++) {
// dp[j][c]+(sumb-dp[j][c])/2
ans = max(ans, min(1.0*dp[j][c]+sumb, 2.0*c));
}
printf("%.10f ", ans/2.0);
}
}

浙公網安備 33010602011771號