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+){
ans=max(A,max(C,B));
return;
}
A+=sum[now];dfs(now+);A-=sum[now];
B+=sum[now];dfs(now+);B-=sum[now];
C+=sum[now];dfs(now+);C-=sum[now];
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&sum[i]);
dfs();
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+){
ans=max(A,max(C,B));
return;
}
A+=sum[now];if(A<ans) dfs(now+);A-=sum[now];
B+=sum[now];if(B<ans) dfs(now+);B-=sum[now];
C+=sum[now];if(C<ans) dfs(now+);C-=sum[now];
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&sum[i]);
dfs();
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[][20][20];
int num[MAXN],sum[MAXN];
int dfs(int now,int x,int y){
int z=sum[now-]-x-y;
if(now==n+) return max(x,max(y,z));
if(dp[now][x][y]) return dp[now][x][y];
dp[now][x][y]=min(dfs(now+,x+num[now],y),min(dfs(now+,x,y+num[now]),dfs(now+,x,y)));
return dp[now][x][y];
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&num[i]);
for(int i=;i<=n;i++) sum[i]=sum[i-]+num[i];
dfs(,,);
cout<<dp[][][];
}

洛谷 P3040 [USACO12JAN]贝尔分享Bale Share的更多相关文章

  1. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  2. 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

    传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...

  3. 洛谷 P3041 [USACO12JAN] Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  4. 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

    https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...

  5. 洛谷P4180 [Beijing2010组队]次小生成树Tree(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)

    洛谷题目传送门 %%%TPLY巨佬和ysner巨佬%%% 他们的题解 思路分析 具体思路都在各位巨佬的题解中.这题做法挺多的,我就不对每个都详细讲了,泛泛而谈吧. 大多数算法都要用kruskal把最小 ...

  6. 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)

    洛谷题目传送门 你谷无题解于是来补一发 随便百度题解,发现了不少诸如树剖\(log^3\)LCT\(log^2\)的可怕描述...... 于是来想想怎么利用题目的性质,把复杂度降下来. 首先,每个点的 ...

  7. 洛谷P4180 [BJWC2010]次小生成树(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)

    洛谷题目传送门 %%%TPLY巨佬和ysner巨佬%%% 他们的题解 思路分析 具体思路都在各位巨佬的题解中.这题做法挺多的,我就不对每个都详细讲了,泛泛而谈吧. 大多数算法都要用kruskal把最小 ...

  8. 在洛谷3369 Treap模板题 中发现的Splay详解

    本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...

  9. [洛谷P1707] 刷题比赛

    洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...

随机推荐

  1. Android configChanges使用方法

    1.    在manifest文件里使用activity的默认属性.横屏竖屏时,惠重复调用onDestory和onCreate  造成不必要的开销.Android默认如此应该是为了适配不同的xml布局 ...

  2. hibernate动态表名映射--仅仅有想不到,没有做不到

    近期的一个项目有一个需求,有N个考核单位,要对每一个考核单位生成一张考核情况表.这样做的目的是横切数据库,这这个需求的实现中,我的组员遇到了一个技术问题,我将我的解决的方法和整个思考过程与大家分享, ...

  3. Android Camera+SurfaceView实现自己定义拍照

    对Activity强制横屏,保证预览方向正确. 使用OrientationEventListener监听设备方向.推断竖拍时,旋转照片后再保存.保证竖拍时预览图片和保存后的图片方向一致. 执行效果: ...

  4. angularjs $http 服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. zzulioj--1828-- 贪心的小猫咪(贪心模拟)

    1828: 贪心的小猫咪 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 14  Solved: 4 SubmitStatusWeb Board Des ...

  6. Python TurtleWorld configuration and simple test

    TurtleWorld provides a set of functions for drawing lines by steering turtles around the screen. You ...

  7. 使用google API之前需要對input 做什麼 安全性的處理?

    我正要使用node.js 和 google map api做一个小应用,Google MAP API的使用URL如下: https://maps.googleapis.com/maps/api/pla ...

  8. MySQL服务正在启动或停止中,请稍候片刻后再试一次【解决方案】

    相信有些小伙伴在使用数据库的过程中会经常频繁的启动和停止MySQL服务,有时候会出现“服务正在启动或停止中,请稍候片刻后再试一次.”这样的提示,如下图所示. 于是乎想办法去解决这个问题,但是发现连强制 ...

  9. MySql系列之初识

    数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件都运 ...

  10. info---Linux下info格式的帮助指令。

    info命令是Linux下info格式的帮助指令. 它的几个常用快捷键. ?键:它就会显示info的常用快捷键. N键:显示(相对于本节点的)下一节点的文档内容. P键:显示(相对于本节点的)前一节点 ...