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. 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

    [064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...

  2. IP地址的规划和设计方法(二)

    五,IP地址规划方法           (1)IP地址规划的基本步骤           网络地址规划须要按下面6步进行:           a)推断用户对网络与主机数的需求:           ...

  3. nodejs简介

    1.Node.js是什么? Node.js不是一种独立的语言,与PHP.java..net即是开发语言也是平台不同,也不是javaScript的框架 jquery,更不是浏览器的库ExtJs,能不与E ...

  4. BZOJ 1927 最小费用流问题

    From lydrainbowcat //By SiriusRen #include <queue> #include <cstdio> #include <cstrin ...

  5. RSA加密的方式和解密方式

    RSAsecurity.java package com.mstf.rsa; import java.security.KeyFactory; import java.security.KeyPair ...

  6. 安装meteor运行基本demo发生错误。

    bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...

  7. windows下远程链接虚拟机Linux下MySQL数据库问题处理

    参考解决:https://www.cnblogs.com/blogforly/p/5997553.html 今天远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对 ...

  8. sql 知识点

    1 聚集索引和非聚集索引的区别 聚集索引是指 表中存储的数据按照索引的顺序来存储,数据检索效率高,但是对数据更新影响较大:非聚集索引是数据存储在一个地方,索引存储在另一个地方:索引带有指针,指向数据的 ...

  9. 最新linux运维高级架构课13期 架构师课程

    有会员购买的,分享给大家.完整一套,可以学习一下.     ├─L001-2017linux运维高级架构师13期-运维与自动化运维发展-10节 │      1-1运维职业发展.avi │      ...

  10. usb芯片调试经验

    记录一下调试usb有关的芯片的一些经验. 1.有i2c的芯片.一般有i2c的地址选择. 检查地址选择是否正确,地址是多少.SCL和SDA上面是否有上拉电阻. 芯片的地址是几位的.I2c的时钟频率也是必 ...