【二分 贪心】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个木块,堆 ...
随机推荐
- ssh 下载文件以及上传文件到服务器
https://blog.csdn.net/jackghq/article/details/64124062 scp john@192.168.1.100:~/Desktop/MHN_error_so ...
- assembly x86(nasm)串比较
预留字符串口令,输入口令串与预留密码串比较.若匹配则显示“MATCH!CONGRATULATION”,否则显示“NOMATCH!”,并让用户重新输入,程序能对口令进行测试,但测试次数最多3次,若3次输 ...
- gcd(2018.10.24)
良心题,暴力枚举即可. 代码: #include<cstdio> #include<cmath> #include<algorithm> using namespa ...
- mysql整理(个人)
注意:以下命令都是在Linux系统下执行的: 1.验证mysql是否安装成功: mysqladmin --version 2.连接mysql服务器: mysql -u root -p 之后输入密码 3 ...
- Tinghua Data Mining 4
贝叶斯 决策树 知道三文鱼和金枪鱼颜色 让你去猜 B命中的概率不能直接减去四分之三 因为有可能同时命中 A B 命中不是互斥事件 即便体检报告是阳性,真正得癌症的概率也很小,只有0.21 绝大多数的阳 ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- 物体检测丨Faster R-CNN详解
这篇文章把Faster R-CNN的原理和实现阐述得非常清楚,于是我在读的时候顺便把他翻译成了中文,如果有错误的地方请大家指出. 原文:http://www.telesens.co/2018/03/1 ...
- phpdesigner 配置SVN
- 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________。A.客观地验证需求管理活动
需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________.A.客观地验证需求管理活动 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是_________ ...
- Java开发笔记(九十六)线程的基本用法
每启动一个程序,操作系统的内存中通常会驻留该程序的一个进程,进程包含了程序的完整代码逻辑.一旦程序退出,进程也就随之结束:反之,一旦强行结束进程,程序也会跟着退出.普通的程序代码是从上往下执行的,遇到 ...