科学二分姿势

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的更多相关文章

  1. bzoj3477[Usaco2014 Mar]Sabotage

    题意 给出一个长为n的正整数序列(n<=1e5),要求选出一个非空前缀和一个非空后缀(这两段不能够加起来组成整个序列),使得这个前缀和后缀中的所有数字一起求平均数的结果最小 分析 最大/最小化平 ...

  2. BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )

    先二分答案m, 然后对于原序列 A[i] = A[i] - m,  然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...

  3. [Usaco2014 Mar]Sabotage

    [Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...

  4. 【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](子段和),如果结果& ...

  5. BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案

    BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...

  6. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  7. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  8. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  9. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

随机推荐

  1. 新装centos 6.5 基本配置

    开机自动联网 vi /etc/sysconfig/network-scripts/ifcfg-eth0; 将ONBOOT=no,改为ONBOOT=yes,保存退出 开机直接进入命令行模式 vi /et ...

  2. JS与JQ的对比与提高

    来吧, 案例1:先上个例子js写的省市二级联动 <!DOCTYPE html><html> <head> <meta charset="UTF-8& ...

  3. bzoj2825:[AHOI2012]收集资源

    传送门 看到数据范围这么小,就没想过暴力的办法么 考虑肯定是从近走到远,所以走的点之间一定没有其他的点,所以我们就可以暴力的建图,然后暴力的去dfs就好了 代码: #include<cstdio ...

  4. JSP技术概念

  5. html 5 video audio

    autoplay autoplay 如果出现该属性,则视频在就绪后马上播放. controls controls 如果出现该属性,则向用户显示控件,比如播放按钮. height pixels 设置视频 ...

  6. 63. 不同路径 II

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网 ...

  7. 牛客练习赛42C(枚举权值)

    传送门 思路:既然无法枚举每个情况,那就枚举每个出现过的权值,加和.那么每个权值出现了多少次呢?用总数减去一次都选不中这个数的次数即可,类似概率的方法. #include <bits/stdc+ ...

  8. Docker与虚拟机

    Docker与虚拟机 简述 Docker 在容器的基础上,进行了进一步的封装,从文件系统.网络互联到进程隔离等等,极大的简化了容器的创建和维护.使得 Docker 技术比虚拟机技术更为轻便.快捷.下面 ...

  9. C++面试中的singleton类

    引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public: ...

  10. aspectj xml

    1.接口和类 1.1 ISomeService 接口 public interface ISomeService { public void doSome(); public void dade(); ...