洛谷P2115 [USACO14MAR]破坏Sabotage
题目描述
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 行: 一个实数, 表示平均牛奶产量的最小值, 保留三位小数 (四舍五入)。
输入输出样例
说明
【样例说明】
移去 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
第一次写实数类型的二分,参考了一下题解
相比于实数类型的二分,这道题的关键在于如何维护最小平均数
设去掉[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。
sum[n]-sum[j]+sum[i-1]<=xn-xj-x(i-1);
(sum[n]-xn)-(sum[j]-xj)+(sum[i-1]-x(i-1))<=0; */
对于(sum[n]-xn)是个常数
对于(sum[i-1]-x(i-1))用一个变量维护
对于(sum[j]-xj)枚举
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>'') {if(c=='-')f=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-,c=getchar();return x*f;
}
int n;
int sum[MAXN];
bool check(double val)
{
double now=123456789.00;
double nowmin=sum[]-val;
double sumn=sum[n]-val*n;
for(int i=;i<=n-;i++)
{
if(sumn-sum[i]+val*i+nowmin<=) return ;
if(sum[i-]-val*(i-)<nowmin) nowmin=sum[i-]-val*(i-);
}
return ;
}
int main()
{
n=read();
for(int i=;i<=n;i++) sum[i]=read(),sum[i]=sum[i]+sum[i-];
double l=,r=,ans=;
while(r-l>1e-)
{
double mid=(r+l)/2.0;
if(check(mid)) r=mid;
else l=mid;
}
printf("%.3lf",r);
return ;
}
洛谷P2115 [USACO14MAR]破坏Sabotage的更多相关文章
- 洛谷2115 [USACO14MAR]破坏Sabotage
https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...
- P2115 [USACO14MAR]破坏Sabotage
题意:给你一个正整数序列,让你删去一段区间内的数[l,r] $1<l\le r <n$ 使得剩余的数平均值最小$n\le 10^5$ 1.不难想到暴力,用前缀和优化$O(n^2)$ #in ...
- 洛谷【P2115】[USACO14MAR]破坏Sabotage
我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.luogu.org/problemnew/show/P21 ...
- 洛谷P2115 Sabotage G 题解
题目 [USACO14MAR]Sabotage G 题解 本蒟蒻又来了,这道题可以用二分答案来解决.我们可以设答案最小平均产奶量为 \(x \ (x \in[1,10000])\) .然后二分搜索 \ ...
- [USACO14MAR]破坏Sabotage 二分答案
题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...
- LuoguP2115 [USACO14MAR]破坏Sabotage【二分答案】By cellur925
本来是想找一道生成树的题做的...结果被洛咕的标签骗到了这题...结果是二分答案与生成树一点mao关系都没有.... 题目大意:给你一个序列,请你删去某一个$l~r$区间的值($2<=i< ...
- 洛谷——P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- [USACO14MAR]破坏Sabotage
还是二分答案,发现我的$check$函数不太一样,来水一发题解 列一下式子 $$\frac{sum-sum[l,r]}{n-(r-l+1)}<=ans$$ 乘过去 $$sum-sum[l,r]& ...
随机推荐
- AngularJs轻松入门(三)MVC架构
MVC应用程序架构最早于1970年起源于Smalltalk语言,后来在桌面应用程序开发中使用较为广泛,如今在WEB开发中也非常流行.MVC的核心思想是將数据的管理(Model).业务逻辑控制(Cont ...
- SQL_触发器学习
--触发器学习-------------------------------------------------------------------------------after 触发器----- ...
- SPFA的小优化
标签:闲扯 SPFA的小优化 1. 向队尾加入元素时,如果它比对首还优,就把把它直接和队首交换. 拿一个双端队列来实现 (手写 , head ,tail STLdeque亲测及其慢) 这个小优化其 ...
- 洛谷P1164 小A点菜 && caioj 1410 动态规划1:点菜(背包方案问题)
方程很简单 f[0] = 1 f[j] += f[j-w[i]] #include<cstdio> #define REP(i, a, b) for(int i = (a); i < ...
- 我的投资案例(3)-看好互联网和金融两大朝阳行业,参投入股垂直金融招聘平台"职业梦CareerDream.cn"
作为一名喜欢读书,关注中国和欧美国家发展的知识青年, 同时作为一名程序员和对金融投资感兴趣的业余爱好者,本人一直看好 以IT互联网为代表的科技和以投资VC为代表的金融,这2大朝阳行业的发展. ...
- malloc()和free()的原理及实现
在C语言中只能通过malloc()和其派生的函数进行动态的申请内存,而实现的根本是通过系统调用实现的(在linux下是通过sbrk()系统调用实现). malloc()到底从哪里得到了内存空间?答案是 ...
- Java实现断点续传。
http://www.cnblogs.com/liaojie970/p/5013790.html
- hdu-1342 Lotto
http://acm.hdu.edu.cn/showproblem.php? pid=1342 题意:以升序的形式给定k个数.输出从中挑选6个数满足升序的全部情况. 思路:两个參数.第一个保存当前搜索 ...
- 基于express+redis高速实现实时在线用户数统计
作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样基于express+redis高速实现实时在线用户数统计. 1. 在github.com上创建项目uv-tj.将其同步到本地: ...
- thinkphp5项目--企业单车网站(七)
thinkphp5项目--企业单车网站(七) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...