洛谷 P3040 [USACO12JAN]贝尔分享Bale Share
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.
输入输出样例
8
14
2
5
15
8
9
20
4
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的更多相关文章
- 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...
- 洛谷 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 ...
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...
- 洛谷P4180 [Beijing2010组队]次小生成树Tree(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)
洛谷题目传送门 %%%TPLY巨佬和ysner巨佬%%% 他们的题解 思路分析 具体思路都在各位巨佬的题解中.这题做法挺多的,我就不对每个都详细讲了,泛泛而谈吧. 大多数算法都要用kruskal把最小 ...
- 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)
洛谷题目传送门 你谷无题解于是来补一发 随便百度题解,发现了不少诸如树剖\(log^3\)LCT\(log^2\)的可怕描述...... 于是来想想怎么利用题目的性质,把复杂度降下来. 首先,每个点的 ...
- 洛谷P4180 [BJWC2010]次小生成树(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)
洛谷题目传送门 %%%TPLY巨佬和ysner巨佬%%% 他们的题解 思路分析 具体思路都在各位巨佬的题解中.这题做法挺多的,我就不对每个都详细讲了,泛泛而谈吧. 大多数算法都要用kruskal把最小 ...
- 在洛谷3369 Treap模板题 中发现的Splay详解
本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...
随机推荐
- 【C#】报表制作<机房重构>
前言 和VB须要引用其它报表软件不同,VS自带报表设计的功能,初次尝试.就感受到了它的强大之处. 报表制作 话不多说.直接报表的制作过程. 1.首先,我们要先制作一个报表的容器.放到我们显示报表的窗口 ...
- AvtiveMQ 参考
推荐学习:https://www.cnblogs.com/zhuxiaojie/p/5564187.html#autoid-2-1-0
- Find and counter
Find: In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting ...
- 相机拍照友盟检测crash是为什么?
友盟报错如下* setObjectForKey: object cannot be nil (key: UIImagePickerControllerOriginalImage)(null)(( 0 ...
- python import windows文件路经
import sys sys.path.append("E:\\python\\workspacepython\\PY001\\src\\testpy01") import str ...
- jsLittle源码封装对象合并
JSLi.extend = JSLi.fn.extend = function () { var options, name, src, copy, target = arguments[0],i = ...
- nil gogo
https://blog.csdn.net/zhonggaorong/article/details/50233421 https://github.com/KevinHM/FunctionalRea ...
- PHPSTORM+Xdebug断点调试代码
如果没有安装 PHPSTORM 可以参考 phpstorm10安装并汉化 一.下载 XDEBUG 下载地址:https://xdebug.org/download.php 二.如何快速寻找适合自己PH ...
- vue组件递归的一些理解
自己做个小项目练手,需要用到组件递归,网上查了一些资料,每个代码片段都认识,但是连起来,就一团浆糊. 既然人傻就多思考吧.不明白的点有以下: 1.组件怎么自己调用自己,函数的递归是就是在functio ...
- gcd步数
题目描述 一个有趣的函数F(a,b),表示对于数对(a,b)调用辗转相除法的步数为多少 例如 (24,40)....0 (16,24).....1 (8,16).....2 (0,8)....3,即f ...