科学二分姿势

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. css 所有选择器 实例与总结

    目录 什么是选择器? 选择器都有那些呢? 标签选择器 ID选择器 类选择器 后代选择器 子代选择器 组合选择器 交集选择器 相邻兄弟选择器 通用兄弟选择器 属性选择器 伪类选择器 什么是选择器? 在c ...

  2. ionic4+angular7+cordova开发入门

    前言 ionic是一个垮平台开发框架,可通过web技术开发出多平台的应用.但只建议开发简单应用.复杂的应用需要用到许多cordova插件,而cordova插件的更新或者移动平台的更新很可能导致插件的不 ...

  3. XML标准和RFC官方文档

  4. python 全局变量 局部变量

    ##全局变量,局部变量#在函数内部可以调用全局变量,不能随意改变全局变量#若要在函数内部改变全局变量,需用关键字global #代码中全局变量都大写,局部变量都小写(非必须,一种规范) P = &qu ...

  5. Memcached 未授权访问漏洞及加固

    memcached是一套分布式的高速缓存系统.它以Key-Value(键值对)形式将数据存储在内存中,这些数据通常是应用读取频繁的.正因为内存中数据的读取远远大于硬盘,因此可以用来加速应用的访问. 漏 ...

  6. CAD 安装时出现.net frameword 3.5安装不上的问题

    右击---我的电脑---功能---.net framework 3.5 ---勾选---安装,然后再进行安装CAD即可

  7. 3、从尾到头打印链表------------>剑指offer系列

    题目 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 分析 要了解链表的数据结构: val属性存储当前的值,next属性存储下一个节点的引用. 要遍历链表就是不断找到当前节点的nex ...

  8. Android Generate Signed APK: Errors while building APK. You can find the errors )

    开发过程中,总会遇到很多坑: Gradle build finished with 101 error(s) in 1m 35s 424ms 19:23:50 Generate Signed APK: ...

  9. Git常用命令的使用方法

    推荐一个比较好的GIT的教学地址,廖雪峰老师的git教程! 这里简述Git常用命令的使用方法: 一.初始化git 右键进入 Git Bash 1.建立身份信息 git config --global ...

  10. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...