BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )

先二分答案m, 然后对于原序列 A[i] = A[i] - m,  然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, sum为原序列的和.

假如二分的答案m是可行的, 那么 L + R = sum - n * m - mx 应该 <= 0

-------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
  
#define rep(i, n) for(int i = 0; i < n; i++)
#define clr(x, c) memset(x, c, sizeof(x))
  
using namespace std;
 
const int maxn = 100009;
const double eps = 1e-3;
 
int A[maxn], n, sum;
double B[maxn], T[maxn];
 
bool check(double m) {
double p = 0, B, mx = -1e10;
for(int i = 1; i < n - 1; i++) {
B = A[i] - m;
if(p >= 0) B += p;
mx = max(mx, p = B);
}
return sum - n * m - mx <= 0;
}
 
int main() {
freopen("test.in", "r", stdin);
cin >> n;
sum = 0;
rep(i, n) {
   scanf("%d", A + i);
   sum += A[i];
}
double L = 1, R = 10000;
while(R - L >= eps) {
double m = L + (R - L) / 2;
check(m) ? R = m : L = m;
}
printf("%.3lf\n", (L + R) / 2);
return 0;
}

------------------------------------------------------------------------------------- 

3477: [Usaco2014 Mar]Sabotage

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 106   Solved: 61
[ Submit][ Status][ Discuss]

Description

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 台机器,第 i 台机器的工作能力为 Ai。保罗阴谋破坏一些机器,使得约翰的
工作效率变低。保罗可以任意选取一段编号连续的机器,使它们停止工作。但这样的破坏只能搞一次,
而且保罗无法破坏第一台或最后一台机器。请问他该破坏哪些机器才能让剩下机器的工作效率的平均
数最小?为了显示存在感,保罗至少必须破坏一台机器。

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
OUTPUT DETAILS: The optimal solution is to remove the 7 and 8, leaving 5, 1, and 2, whose average is 8/3.

HINT

Source

 

你可能感兴趣的:(USACO)