洛谷 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. 洛谷P2508 [HAOI2008]圆上的整点

    题目描述 求一个给定的圆$ (x^2+y^2=r^2) $,在圆周上有多少个点的坐标是整数. 输入格式 \(r\) 输出格式 整点个数 输入输出样例 输入 4 输出 4 说明/提示 \(n\le 20 ...

  2. keras.preprocessing.text.Tokenizer

    说明:num_words的参数设置,对应着sequences_to_matrix方法返回的arrray的shape[1],用于约束返回数组的第2个维度.对texts_to_sequences(text ...

  3. html-加水印--watermark--代码测试

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 【Zabbix】zabora批量部署

    zabora简化批量部署 目的:简化部署zabora,批量监控数据库的常用指标 1 数据库用户赋权 上传cre_arp_monitor.sh,并且部署用户. [root@oradb ~]# chown ...

  5. 阿里OSS 渗透案例

    采用JavaScript客户端直接签名时,AccessKeyID和AcessKeySecret会暴露在前端页面,因此存在严重的安全隐患. 渗透案例 阿里云Access Token问题 - 项目收获记录 ...

  6. 【10】Nginx:后面有无 / 的区别

    写在前面的话 在 nginx 中,我们很多时候都有一个疑问,在 proxy_pass 或者 root 或者 location 后面需不需要加上 /,加和不加有啥区别. root  / alias 后面 ...

  7. Mysql设置binlog过期时间并自动删除

    问题: Mysql数据库由于业务原因,数据量增长迅速,binlog日志会增加较多,占用大部分磁盘空间. 解决方案: 出于节约空间考虑,可进行删除多余binary日志,并设置定期删除操作. .查看bin ...

  8. [Docker] Win10中安装Docker并运行Nginx镜像

    一.安装Docker 进入官网:https://www.docker.com/products/docker-desktop 可能需要先注册登录,很简单的. 点击 Download Desktop f ...

  9. 基于Spark的电影推荐系统(推荐系统~1)

    第四部分-推荐系统-项目介绍 行业背景: 快速:Apache Spark以内存计算为核心 通用 :一站式解决各个问题,ADHOC SQL查询,流计算,数据挖掘,图计算 完整的生态圈 只要掌握Spark ...

  10. C#,二分法,BinarySearch()

    static int BinarySearch(int[] arr,int key,int low,int high) { low = 0;high = arr.Length - 1; while(l ...