洛谷 P2115 [USACO14MAR]破坏Sabotage

https://www.luogu.org/problem/P2115

JDOJ 2418: USACO 2014 Mar Gold 2.Sabotage

https://neooj.com/oldoj/problem.php?id=2418

Description

Problem 2: Sabotage [Brian Dean, 2014]

Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer
John's milking equipment!

FP是FJ的死对头,他想破坏FJ的挤奶系统。

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.

FJ的挤奶系统包含一行共N(3 <= N <= 100,000)个挤奶器,其中第i号挤奶器可以挤M_i(1<= M_i <= 10,000)升牛奶。
FP打算破坏从第i号到第j号这连续的一段挤奶器(2 <=i <= j <= N-1),但保罗不打算破坏第1号和第N号挤奶器。
FP想要达到的目的是使得剩下的挤奶器的平均产量尽可能低。FP至少要破坏一个挤奶器。

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.

求这个最低的平均产量。

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.

Sample Input

5
5
1
7
8
2

Sample Output

2.667

HINT

OUTPUT DETAILS:

The optimal solution is to remove the 7 and 8, leaving 5, 1, and 2, whose
average is 8/3.

 
我个人觉得这题像是一个数学题。
因为是一段连续的数列,所以自然而然地想到维护一个前缀和数组,并且采用二分答案来优化枚举平均值。
对于每一个平均值,我们怎么判断呢?
设去掉[i,j]区间,去掉的和就是sum[j]-sum[i-1],剩下的和就是sum[n]-(sum[j]-sum[i-1]),去括号,sum[n]-sum[j]+sum[i-1]
也就是[j,n]的和加上[1,i-1]的和
剩下的个数n-(j-i+1)
 
所以我们得出了判断条件:
(sum[n]-sum[j]+sum[i-1])/(n-j+i-1)<=x
 
然后运用浮点型二分来AC:
 
#include<cstdio>
#include<algorithm>
using namespace std;
int sum[];
int n;
bool check(double x)
{
double minv = sum[]-x*;
for(int i=;i<n;i++)
{
if (sum[n]-x*n-(sum[i]-x*i)+minv<=)
return ;
minv=min(minv,sum[i]-x*i);
}
return ;
}
int main()
{
scanf("%d",&n);
for (int i=; i<=n; i++)
{
int a;
scanf("%d",&a);
sum[i] = sum[i-]+a;
}
double l = , r = ;
while(r-l>1e-)
{
double mid = (l+r)/;
if (check(mid))
r = mid;
else
l = mid;
}
printf("%.3lf",r);
return ;
}

USACO Sabotage的更多相关文章

  1. USACO Sabotage, 2014 Mar 破坏阴谋(二分+贪心)

    一开始看完这题就有个想法: 只要把大于整个序列平均数的最大连续序列就是最优? 那把整个序列都减掉平均数 在做最大连续字序列和且记录长度? 仔细思考一下并不太对: 当子序列最大但长度较大 也许也比不上删 ...

  2. USACO翻译:USACO 2014 MARCH GOLD P2 Sabotage

    1.破坏{DOLD题2} sabotage.pas/c/cpp [问题描述] 农夫约翰的头号敌人保罗决定破坏农民约翰的挤奶设备.挤奶设备排成一行,共N(3<= N <=100000)台挤奶 ...

  3. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  4. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  5. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  6. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  7. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  8. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  9. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

随机推荐

  1. InvalidProgramException

    InvalidProgramException 这tmd是个什么错,我现在都想不起这个exception是怎么触发的了. 后来google了一下,发现是.net 2.0的编译器的bug,和内存或堆栈使 ...

  2. bayer2bmp

    #include <stdlib.h> #include <string.h> #include <getopt.h> #include <stdint.h& ...

  3. Kubernetes容器集群管理环境 - Node节点的移除与加入

    一.如何从Kubernetes集群中移除Node比如从集群中移除k8s-node03这个Node节点,做法如下: 1)先在master节点查看Node情况 [root@k8s-master01 ~]# ...

  4. Eureka配置

    介绍 SpringCloud是一个完整的微服务治理框架,包括服务发现和注册,服务网关,熔断,限流,负载均衡和链路跟踪等组件. SpringCloud-Eureka主要提供服务注册和发现功能.本文提供了 ...

  5. JSP是Servlet详解

    前言:前一段时间写了好多Servlet和JSP相关的博客,自以为理解的差不多了,岂不知人外有人,天外有天,代码外还有源码,受高人点拨,看了一下Servlet源码,感触颇深,再也不敢说懂了,不明白生活的 ...

  6. C# NuGet常用命令

    命令执行位置:工具=〉Nuget包管理器=〉程序包管理器控制台 一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安 ...

  7. Web Api全局预防Xss攻击

    本文转载自https://www.cnblogs.com/ruanyifeng/p/4739807.html.对第二种过滤方法的代码进行了一些修改和注释,记录一下免得以后忘了.已经测试过,应该可以直接 ...

  8. OpenJDK下SpringBoot使用HttpSession时页面打开卡住

    近期将一个老项目向ARM版的CentOS7移植时,遇到了SpringBoot启动顺利,但访问页面卡住的问题.由于是aarch64架构,因此使用了openjdk,这个项目之前在x86_64环境下一直是用 ...

  9. python之三方库(冷门+热门)

    AES加密库 pycryptodome

  10. Python requests库的使用(二)

    1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions        #引入exc ...