【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage
科学二分姿势
Description
Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipment! The milking equipment consists of a row of N (3 <= N <= 100,000) milking machines, where the ith machine produces M_i units of milk (1 <= M_i <= 10,000). Farmer Paul plans to disconnect a contiguous block of these machines -- from the ith machine up to the jth machine (2 <= i <= j <= N-1); note that Farmer Paul does not want to disconnect either the first or the last machine, since this will make his plot too easy to discover. Farmer Paul's goal is to minimize the average milk production of the remaining machines. Farmer Paul plans to remove at least 1 cow, even if it would be better for him to avoid sabotage entirely. Fortunately, Farmer John has learned of Farmer Paul's evil plot, and he is wondering how bad his milk production will suffer if the plot succeeds. Please help Farmer John figure out the minimum average milk production of the remaining machines if Farmer Paul does succeed.
约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai。保罗阴谋破坏一些机器,使得约翰的
工作效率变低。保罗可以任意选取一段编号连续的机器,使它们停止工作。但这样的破坏只能搞一次,
而且保罗无法破坏第一台或最后一台机器。请问他该破坏哪些机器才能让剩下机器的工作效率的平均
数最小?为了显示存在感,保罗至少必须破坏一台机器。
Input
* Line 1: The integer N.
* Lines 2..1+N: Line i+1 contains M_i.
Output
* Line 1: The lowest possible average Farmer Paul can achieve, rounded to 3 digits after the decimal point, and printed with 3 digits after the decimal point.
题目分析
显然极大/极小化特定值是要用二分的。但是这里要求平均数应该如何check呢?
check的基本模式当然就是依靠贪心地选取元素来判断是否满足限制。这里如果按照普通的思路求平均数,由于其受到所选元素个数的限制,check会变得不可做。
但是!我们有一种酷炫操作:二分平均数$x$没错,然后将每一个元素都减去$x$。这样一来,check要做的就是在原序列中找出一段前缀和一段后缀使得它们的和<=0.
我是直接写前后缀的,这样非常慢。
网上有一种更妙的方法就是等价于在序列中找最大子段和。
#include<bits/stdc++.h>
const int maxn = ;
const double eps = 1e-; int n,a[maxn];
double f[maxn],g[maxn],b[maxn],ans; bool check(double x)
{
f[] = g[n+] = ;
for (int i=; i<=n; i++) b[i] = a[i]-x, f[i] = , g[i] = ;
for (int i=; i<=n; i++) f[i] = f[i-]+b[i];
for (int i=n; i>=; i--) g[i] = g[i+]+b[i];
f[] = g[n+] = 2e9;
for (int i=; i<=n; i++) f[i] = std::min(f[i], f[i-]);
for (int i=n; i>=; i--) g[i] = std::min(g[i], g[i+]);
for (int i=; i<n-; i++)
if (f[i]+g[i+] < eps)
return ;
return ;
}
int main()
{
scanf("%d",&n);
for (int i=; i<=n; i++) scanf("%d",&a[i]);
double l = , ans = r = 10000.0;
for (double mid=(l+r)/; r-l > eps; mid=(l+r)/)
if (check(mid)) ans = mid, r = mid-eps;
else l = mid+eps;
if (n==) ans = std::min(ans, (a[]+a[])/2.0);
printf("%.3lf\n",ans);
return ;
}
END
【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage的更多相关文章
- bzoj3477[Usaco2014 Mar]Sabotage
题意 给出一个长为n的正整数序列(n<=1e5),要求选出一个非空前缀和一个非空后缀(这两段不能够加起来组成整个序列),使得这个前缀和后缀中的所有数字一起求平均数的结果最小 分析 最大/最小化平 ...
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- [Usaco2014 Mar]Sabotage
[Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...
- 【bzoj】3477: [Usaco2014 Mar]Sabotage 01分数规划
这题算是01分数规划吧2333 sum-a[i]*x[i]=c*(n-x[i]) 化简一下就是sum-(a[i]-c)*x[i]-nc=0,每次找最大的(a[i]-c)*x[i](子段和),如果结果& ...
- BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案
BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
随机推荐
- Matlab图像处理相关
相关函数: 读取:imread() %参数为文件名(路径)或url,格式等 写入:imwrite() %参数为写入数据矩阵,写入文件名(路径),格式等 显示:imshow() %显示由输入决定,属性自 ...
- java多线程模拟红绿灯案例
代码Lighter.java: package pack1; /** * 灯线程 * @author Administrator * */ public class Lighter extends T ...
- HDU4405(期望dp)
标准期望套路,很水.读题看好是到n就可以停止了. ; int n, m; db dp[maxn]; map<int, int> mp; int main() { while (~scanf ...
- Codeforces Round #396 (Div. 2) D
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...
- Ubuntu-通过v2版本的rancher安装部署k8s
环境: ubuntu:16.04+(64位) CPU:2C MEM:>4G docker:17.03.2 1.13.1 1.12.6 基础配置:(若是云服务器,下列只需要放行端口) >&g ...
- 转 在shell脚本中使用expect实现scp传输问题
1.安装expect expect用于shell脚本中自动交互,其是基于tcl编程语言的工具.所以安装expect首先安装tcl.本文中使用的是expect5.45和tcl8.6.6. 安装tcl [ ...
- 《javascript设计模式》笔记之第四章:继承
一:首先,一个简单的继承实例: 首先是创建一个父类Person: function Person(name) { this.name = name; } Person.prototype.getNam ...
- Ice-cream Tycoon9(线段树)
线段树的一些基本应用,就是函数写了很多,有点繁琐. 以每个物品的单价建树,刚开始写了个裸的想水过去直接MLE了,然后又离散化了下. 离散化单价后建树,lz数组用来清零,s数组保存结点所含物品个数,co ...
- mysql添加用户并赋予权限命令
添加用户: create user 'gouge'@'localhost' identified by 'gouge'; 赋予权限: 给gouge 用户赋予所有test开头的数据库权限 (test% ...
- 分布式系统ID生成办法
前言 一般单机或者单数据库的项目可能规模比较小,适应的场景也比较有限,平台的访问量和业务量都较小,业务ID的生成方式比较原始但是够用,它并没有给这样的系统带来问题和瓶颈,所以这种情况下我们并没有对此给 ...