1. 程式人生 > >洛谷 P3040 [USACO12JAN]貝爾分享Bale Share

洛谷 P3040 [USACO12JAN]貝爾分享Bale Share

ges min 貝爾 clas -o ans log org ota

P3040 [USACO12JAN]貝爾分享Bale Share

題目描述

Farmer John has just received a new shipment of N (1 <= N <= 20) bales of hay, where bale i has size S_i (1 <= S_i <= 100). He wants to divide the bales between his three barns as fairly as possible.

After some careful thought, FJ decides that a "fair" division of the hay bales should make the largest share as small as possible. That is, if B_1, B_2, and B_3 are the total sizes of all the bales placed in barns 1, 2, and 3, respectively (where B_1 >= B_2 >= B_3), then FJ wants to make B_1 as small as possible.

For example, if there are 8 bales in these sizes:

2 4 5 8 9 14 15 20

A fair solution is

Barn 1: 2 9 15   B_1 = 26 
Barn 2: 4 8 14   B_2 = 26 
Barn 3: 5 20     B_3 = 25 
Please help FJ determine the value of B_1 for a fair division of the hay bales. 

FJ有N (1 <= N <= 20)包幹草,幹草i的重量是 S_i (1 <= S_i <= 100),他想盡可能平均地將幹草分給3個農場。

他希望分配後的幹草重量最大值盡可能地小,比如, B_1,B_2和 B_3是分配後的三個值,假設B_1 >= B_2 >= B_3,則他希望B_1的值盡可能地小。

例如:8包幹草的重量分別是:2 4 5 8 9 14 15 20,一種滿足要求的分配方案是

農場 1: 2 9 15 B_1 = 26

農場 2: 4 8 14 B_2 = 26

農場 3: 5 20 B_3 = 25

請幫助FJ計算B_1的值。

輸入輸出格式

輸入格式:

  • Line 1: The number of bales, N.

  • Lines 2..1+N: Line i+1 contains S_i, the size of the ith bale.

輸出格式:

  • Line 1: Please output the value of B_1 in a fair division of the hay bales.

輸入輸出樣例

輸入樣例#1: 復制
8 
14 
2 
5 
15 
8 
9 
20 
4 
輸出樣例#1: 復制
26 
技術分享圖片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 30
using namespace std;
int n;
int A,B,C;
int ans=0x7f7f7f7fl;
int sum[MAXN];
void dfs(int now){
    if(max(A,max(B,C))>ans)    return ;
    if(now==n+1){
        ans=max(A,max(C,B));
        return;
    }    
    A+=sum[now];dfs(now+1);A-=sum[now]; 
    B+=sum[now];dfs(now+1);B-=sum[now]; 
    C+=sum[now];dfs(now+1);C-=sum[now];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&sum[i]);
    dfs(1);
    cout<<ans;
}
60分的dfs 技術分享圖片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 30
using namespace std;
int n;
int A,B,C;
int ans=0x7f7f7f7fl;
int sum[MAXN];
void dfs(int now){
    if(now==n+1){
        ans=max(A,max(C,B));
        return;
    }
    A+=sum[now];if(A<ans)    dfs(now+1);A-=sum[now]; 
    B+=sum[now];if(B<ans)    dfs(now+1);B-=sum[now]; 
    C+=sum[now];if(C<ans)    dfs(now+1);C-=sum[now];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&sum[i]);
    dfs(1);
    cout<<ans;
}
AC的dfs

正解思路:動態規劃。

f[i][j][k]表示到第i堆幹草為止,第一個農場分到j的幹草,第二個農場分到k的幹草。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 10010
using namespace std;
int n;
int dp[23][2010][2010];
int num[MAXN],sum[MAXN];
int dfs(int now,int x,int y){
    int z=sum[now-1]-x-y;
    if(now==n+1)    return max(x,max(y,z));
    if(dp[now][x][y])    return dp[now][x][y];
    dp[now][x][y]=min(dfs(now+1,x+num[now],y),min(dfs(now+1,x,y+num[now]),dfs(now+1,x,y)));
    return dp[now][x][y];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)    scanf("%d",&num[i]);
    for(int i=1;i<=n;i++)    sum[i]=sum[i-1]+num[i];
    dfs(1,0,0);
    cout<<dp[1][0][0];
}

洛谷 P3040 [USACO12JAN]貝爾分享Bale Share