【二分 贪心】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个木块,堆 ...
随机推荐
- log4j.xml中Filter的用法
前言 log4j中常用的Filter分为四种:DenyAllFilter.LevelMatchFilter.LevelRangeFilter.StringMatchFilter. 当appender匹 ...
- js动态实现文本框不可编辑状态
两种方法: $("#id").attr("readOnly",false); 不可编辑,可以传值 $("#id").attr("d ...
- [题解](约数)BZOJ_1053_反素数
三条引理:1.1~N中最大风反质数,就是1~N中约数个数最多的最小的一个 比较显然,是应该看出来的一条 2.1~N中任何数的不同因子都不会超过10个,且所有质因子的指数之和不超过30: 2*3*5*7 ...
- 牛客寒假6-J.迷宫
链接:https://ac.nowcoder.com/acm/contest/332/J 题意: 你在一个 n 行 m 列的网格迷宫中,迷宫的每一格要么为空,要么有一个障碍. 你当前在第 r 行第 c ...
- 网络流--Dinic(自用,勿看)
注意:这是一篇个人学习笔记,如果有人因为某些原因点了进来并且要看一下,请一定谨慎地阅读,因为可能存在各种奇怪的错误,如果有人发现错误请指出谢谢! https://www.luogu.org/probl ...
- 转 怎样解读10046 trace (tkprof 的结果 )
set autot on SQL> set autotraceUsage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ ...
- getAnnotation为null的坑
在写一个基于SpringAOP的权限控制的. 自己定义了一个注解,然后逻辑代码需要通过获取自定义注解的一个属性来进行权限控制. 下面简单上一下关键代码: 自定义注解: @Documented //有关 ...
- [已读]JavaScript高级程序设计(第2版)
经典红皮书~~
- js判断网页访问设备类型
有时候我们会需要来根据不同的设备访问进行不同的操作,在网上找了一下,主要是根据Navigator对象, if(/Android|Windows Phone|webOS|iPhone|iPod|Blac ...
- #118. 【UR #8】赴京赶考
链接:#118. [UR #8]赴京赶考 高中,高中,短暂的三年.NOI是高中结业考试,而高考在每年暑假举行. 高二暑假,这是你最后一次参加高考的机会.你已经为了高考停课很久了,OI的知识很久没管了. ...