[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
前缀和很容易想到;
然后推一推式子,二分答案看是否可行;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int a[maxn];
int sum[maxn];
double ans = inf * 1.0;
bool check(double k) {
double minn = sum[1] - k * 1.0;
for (int i = 2; i < n; i++) {
if ((sum[n] - k * n - (sum[i] - k * i) + minn) <= 0)return true;
minn = min(minn, sum[i] - k * i);
}
return false;
} int main() {
//ios::sync_with_stdio(0);
rdint(n);
for (int i = 1; i <= n; i++)rdint(a[i]); for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i];
}
double l = 0, r = 1e7 * 1.0;
while (r - l > 1e-5) {
double mid = (l + r) / 2.0;
if (check(mid))r = mid;
else l = mid;
}
printf("%.3lf\n", 1.0*l);
return 0;
}
[USACO14MAR]破坏Sabotage 二分答案的更多相关文章
- [USACO14MAR] 破坏Sabotage(二分答案,分数规划)
题目链接 Solution 去掉中间一段区间 \([l,r]\) 后剩下的平均值可以表示为 : \[\frac{\sum^{n}_{i=1}{v_i}-\sum^{r}_{i=l}{v_i}}{n-( ...
- P2115 [USACO14MAR]破坏(二分答案)
给定一串数,问删除中间一段,剩下的平均数最小是多少: 不容易想到这是个二分. $solution:$ 来手玩一点式子: 首先很容易想到一个前缀和$sum_i $表示i到1的前缀和,这样就能很容易地O( ...
- [USACO14MAR] Sabotage 二分答案 分数规划
[USACO14MAR] Sabotage 二分答案 分数规划 最终答案的式子: \[ \frac{sum-sum[l,r]}{n-len[l,r]}\le ans \] 转换一下: \[ sum[1 ...
- LuoguP2115 [USACO14MAR]破坏Sabotage【二分答案】By cellur925
本来是想找一道生成树的题做的...结果被洛咕的标签骗到了这题...结果是二分答案与生成树一点mao关系都没有.... 题目大意:给你一个序列,请你删去某一个$l~r$区间的值($2<=i< ...
- [USACO14MAR]破坏Sabotage
还是二分答案,发现我的$check$函数不太一样,来水一发题解 列一下式子 $$\frac{sum-sum[l,r]}{n-(r-l+1)}<=ans$$ 乘过去 $$sum-sum[l,r]& ...
- P2115 [USACO14MAR]破坏Sabotage
题意:给你一个正整数序列,让你删去一段区间内的数[l,r] $1<l\le r <n$ 使得剩余的数平均值最小$n\le 10^5$ 1.不难想到暴力,用前缀和优化$O(n^2)$ #in ...
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- 洛谷2115 [USACO14MAR]破坏Sabotage
https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...
- 洛谷P2115 [USACO14MAR]破坏Sabotage
题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...
随机推荐
- codeforces 628B B. New Skateboard (数论)
B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- yahoo的30条优化规则
1.尽量减少HTTP请求次数 终端用户响应的时间中,有80%用于下载各项内容.这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通过减少页面中的元素可以减少HTTP请求的次数.这是提高网页速 ...
- Windows PCM音频捕获与播放实现
在WINDOWS下,音频函数有多种类型,如MCI.多媒体OLE控制.高级音频等,使用方法都比较简单.但如果想编写一个功能较强大的音频处理程序,那就必须使用低级音频函数和多媒体文件I/O来控制音频设备的 ...
- 利用stomp.js实现websocket功能,接收ActiveMQ消息队列
一.ActiveMQ消息发送端 package lixj; import java.util.Date; import javax.jms.Connection; import javax.jms.C ...
- 设置Suse linux 用户远程登录超时时间
执行 # echo "export TMOUT=900" >> /etc/profile 查询设置结果: # cat /etc/profile|grep TMOU ...
- Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N x M的矩阵,请你数一数其中有多少个3 x 3的子矩阵可以构成三阶幻方? 如果3 x 3的矩阵中每一行.每一列 ...
- bzoj 3533: [Sdoi2014]向量集 线段树维护凸包
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3533 题解: 首先我们把这些向量都平移到原点.这样我们就发现: 对于每次询问所得到的an ...
- MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...
- Linux(C/C++)下的文件操作open、fopen与freopen via Boblim
Linux(C/C++)下的文件操作open.fopen与freopen open是linux下的底层系统调用函数,fopen与freopen c/c++下的标准I/O库函数,带输入/输出缓冲. li ...
- java验证,”支持6-20个字母、数字、下划线或减号,以字母开头“这个的正则表达式怎么写?
转自:https://yq.aliyun.com/wenzhang/show_96854 问题描述 java验证,”支持6-20个字母.数字.下划线或减号,以字母开头“这个的正则表达式怎么写? 验证” ...