洛谷 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. linux shell攻略学习笔记一 基础篇

    1.#!/bin/bash shebang 可以自定义 比如 #!/bin/bash +x 就会打印出执行日志 linux中 \ 代表null \n2\n3” 会转义其中的\n,生成3行数据 $! 保 ...

  2. Erlang基础2

    1. apply apply(Mod, Func, [Arg1, Arg2, ..., ArgN]) 等价于 Mod:Func(Arg1, Arg2, ..., ArgN) 区别在于,使用apply, ...

  3. 动手学深度学习11- 多层感知机pytorch简洁实现

    多层感知机的简洁实现 定义模型 读取数据并训练数据 损失函数 定义优化算法 小结 多层感知机的简洁实现 import torch from torch import nn from torch.nn ...

  4. 行为驱动:Cucumber + Java - 实现数据的参数化

    1.什么是参数化 实际设计测试用例过程中,我们经常会用等价类.边界值这样的方法,针对一个功能进行测试数据上的测试,比如一个输入框,正向数据.逆向数据,非法输入等等 2.Cucumber的数据驱动 同上 ...

  5. hdu-6071 Lazy Running

    In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule ...

  6. POJ-2006 Litmus Test 高精度

    The pH scale measures the concentration of protons (H +) in a solution and, therefore, its acidity o ...

  7. Autoware 培训笔记 No. 1——构建点云地图

    1. 首记 相信许多刚开始玩无人驾驶的人都用过Autoware,对runtime manager都比较熟悉,虽然可以通过各种渠道了解到有些设置,甚至有些设置的app下参数的含义,但是,在真车的使用过程 ...

  8. Neo4j 第十篇:更新数据

    更新图包括图的节点和关系的创建.更新和删除,也能更新图的节点和关系的属性.节点标签和关系类型. 一,创建节点 1,创建空的节点 CREATE (n) CREATE (a),(b) 2,创建带标签的节点 ...

  9. 用T4生成EF框架下的DAL、IDAL、BLL、IBLL

    (一)什么是T4模板? T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何 ...

  10. 今日头条3面,被泄露的Java最新面试题

    一.面试第 1 轮 1. linux 网络模型 2. b+树 3. 阻塞队列 4. redis 和 MongoDB 的区别.几个概念对比,还有底层实现. 5. 算法题: merge k sorted ...