[USACO14MAR]Sabotage G

题目描述

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(3<= N <=100000)台挤奶机,其中第i个台挤奶机生产M_i单位(1 <= M_i<=10,000)的牛奶。

保罗计划切断一段连续的挤奶机,从第i台挤奶机到第j台挤奶机(2<= i<= j<= N-1)。注意,他不希望断开第一台或最后一台挤奶机,因为这将会使他的计划太容易被发现。保罗的目标是让其余机器的平均产奶量最小。保罗计划除去至少1台挤奶机。

请计算剩余机器的最小平均产奶量。

输入格式

第 1 行:一个整数 N。

第 2 到 N+1 行:第 i+1 行包含一个整数 M_i。

输出格式

第 1 行: 一个实数, 表示平均牛奶产量的最小值, 保留三位小数 (四舍五入)。

样例 #1

样例输入 #1

5
5
1
7
8
2

样例输出 #1

2.667

提示

【样例说明】

移去 7 和 8,剩下 5, 1, 2,平均值为 8/3。

【数据规模和约定】

对于 30%的数据,N <= 1,000。

对于 50%的数据,N <= 10,000。

对于 100%的数据,3 <= N <= 100,000,1 <= M_i <= 10,000。

【时空限制】

0.2s/128M

解析

非常好的分数规划。容易想到二分答案。这种实数域上的二分,往往枚举二分次数更精确、更好写。

二分 m m m ,也就是最终输出的最小平均数。不难想到, c h e c k check check 以下内容:
s − ( s u m r − s u m l ) n − ( r − l ) ≤ m \dfrac{s-(sum_r-sum_l)}{n-(r-l)} \leq m n−(r−l)s−(sumr​−suml​)​≤m

s − s u m r + s u m l ≤ m n − m r + m l s-sum_r+sum_l\leq mn-mr+ml s−sumr​+suml​≤mn−mr+ml

s − m n ≤ ( s u m r − m r ) − ( s u m l − m l ) s-mn\leq (sum_r-mr)-(sum_l-ml) s−mn≤(sumr​−mr)−(suml​−ml)

对于以上式子, s s s 代表 a a a 数组总和, s u m i sum_i sumi​ 代表 0 − i 0-i 0−i 的前缀和。

上式可简化为:
s − m n ≤ ∑ i = l + 1 r ( a i − m ) s-mn\leq \sum^{r}_{i=l+1}(a_i-m) s−mn≤i=l+1∑r​(ai​−m)
m a x ( ∑ i = l + 1 r ( a i − m ) ) max(\sum^{r}_{i=l+1}(a_i-m)) max(∑i=l+1r​(ai​−m)) 可以使用 DP 的最大连续子段和快速求出。

代码

// E
#include <bits/stdc++.h>
#define int long long
#define SIZE 100010
#define all(x) x.begin(), x.end()
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;inline int read()
{int k=0, f=1;char c=getchar();while(c<'0' || c>'9'){if(c=='-') f=-1; c=getchar();}while(c>='0' && c<='9'){k=k*10+c-48; c=getchar();}return k*f;
}inline void write(int n)
{if(n==0) putchar('0');stack<int> S;while(n)S.push(n%10), n/=10;while(!S.empty()){int x=S.top(); S.pop();putchar(x+48);}
}int n;
int a[SIZE];
int sum;bool ok(double mid)
{double remain=sum-mid*n;double Max=INT_MIN, s=0;for(int i=1; i<n-1; i++){s+=a[i]-mid;Max=max(Max, s);if(s<0) s=0;}return Max>=remain;
}signed main()
{n=read();for(int i=0; i<n; i++)a[i]=read();sum=accumulate(a, a+n, 0);double l=0, r=INT_MAX;double res;for(int tt=0; tt<50; tt++){double m=(l+r)/2;if(ok(m)){res=m;r=m;}elsel=m;}cout<<fixed<<setprecision(3)<<res;return 0;
}

[USACO14MAR]Sabotage G相关推荐

  1. P2115 [USACO14MAR]Sabotage G(二分)

    P2115 [USACO14MAR]Sabotage G(二分) 经典分数规划二分题. 这里注意解释下原理. 假设当前二分的答案是x,实际的答案是ans. 显然若x满足条件,则 x<=ans.我 ...

  2. 解题:USACO14MAR Sabotage

    题面 题外话:我的实数二分有什么问题=.= 仍然(我为什么要这么说)是二分答案,如何检查呢?将所有的数减去二分出来的$mid$后求和得到和$sum$,然后如果在减出来的数列中能找出一段大于$sum$的 ...

  3. 文件上传之伪Ajax方式上传

    From: <由 Windows Internet Explorer 8 保存> Subject: =?gb2312?B?zsS8/snPtKvWrs6xQWpheLe9yr3Jz7SrI ...

  4. P2115 [USACO14MAR]破坏Sabotage

    P2115 [USACO14MAR]破坏Sabotage 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage F ...

  5. [USACO14MAR]破坏Sabotage

    题目链接:[USACO14MAR]破坏Sabotage 首先肯定二分最小值. 然后找到是否存在一个前缀min和后缀min之和小于0即可. AC代码: #pragma GCC optimize(&quo ...

  6. USACO Sabotage

    洛谷 P2115 [USACO14MAR]破坏Sabotage https://www.luogu.org/problem/P2115 JDOJ 2418: USACO 2014 Mar Gold 2 ...

  7. 过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件

    过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件 find ./ -type f -size +1G| xargs rm 在清理系统日志文件 ...

  8. gcc 自动识别的文件扩展名,gcc/g++ -x 选项指定语言,不同 gcc 版本 -std 编译选项支持列表

    对于执行 C 或者 C++ 程序,需要借助 gcc(g++)指令来调用 GCC 编译器. 对于以 .c 为扩展名的文件,GCC 会自动将其视为 C 源代码文件 对于以 .cpp 为扩展名的文件,GCC ...

  9. gcc 和 g++ 的联系和区别,使用 gcc 编译 c++

    GCC 编译器已经为我们提供了调用它的接口,对于 C 语言或者 C++ 程序,可以通过执行 gcc 或者 g++ 指令来调用 GCC 编译器. 实际使用中我们更习惯使用 gcc 指令编译 C 语言程序 ...

最新文章

  1. 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)...
  2. VuePress 入门
  3. vue1与vue2的路由 以及vue2项目大概了解
  4. Springcloud中的region和zone的使用
  5. Android开发之选项菜单(optinosMenu)
  6. 【LeetCode笔记】剑指Offer 41. 数据流中的中位数(Java、堆、优先队列、知识点)
  7. MaxCompute Hash Clustering介绍
  8. SQL Server中的T-SQL元数据功能的完整指南
  9. mysql 高可用架构 proxysql 之一 yum安装
  10. [转载] Python中while循环的基本用法
  11. MySQL集群(四)之keepalived实现mysql双主高可用
  12. APP开发手记01(app与web的困惑)
  13. easyui 全部图标
  14. 命令查看(获取)本机IP地址
  15. 数学符号读法及常用符号英文(超全)
  16. 【微信小程序】云函数入门(保姆级别)
  17. 文献阅读2019-Computer-aided diagnosis system for breast ultrasound images using deep learning
  18. 【Java.JMS】JMS基础
  19. 数据库系统概念 引言(一)
  20. 使用树莓派4b和OpenCV做机械臂夹取

热门文章

  1. 8月英杰传!看看有没你认识的?
  2. 世界上第一款启用 QMK/VIA 的薄型无线机械键盘 Keychron K3 Pro 我刚刚入手
  3. 野蛮时代一直显示无法连接服务器,Euro Truck Evolution (Simulator)无法连接服务器是什么原因...
  4. 戴尔服务器代码查询维修,DELL服务器故障查询表.doc
  5. Redis内存淘汰策略
  6. 使用addRoutes动态添加路由
  7. 198. House Robber抢劫房子Python Java
  8. python中IO库中StringIO方法和BytesIO方法用法详解
  9. php中使用$_ENV获取环境变量
  10. C语言实验——保留整数