Best Cow Fences
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16945 | Accepted: 5425 |
Description
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.
FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.
Calculate the fence placement that maximizes the average, given the constraint.
FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.
Calculate the fence placement that maximizes the average, given the constraint.
Input
* Line 1: Two space-separated integers, N and F.
* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.
* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.
Output
* Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields.
Sample Input
10 6 6 4 2 10 3 8 5 9 4 1
Sample Output
6500
Source
OJ-ID:
poj-2018
author:
Caution_X
date of submission:
20191118
tags:
二分
description modelling:
给定正整数数列A[],求一个平均值最大的,长度不小于L的(连续的)子段,输出该平均值*1000
major steps to solve it:
(1)二分枚举所有的平均值
(2)每一次二分,将A[]-mid,之后求前缀和。判断所取子段平均值是否大于等于mid只要判断选取区间累加是否大于等于0即可
AC code:
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; double a[100005],b[100005]; double sum[100005]; double eps=1e-5; int main() { //freopen("input.txt","r",stdin); int N,L; scanf("%d%d",&N,&L); for(int i=1;i<=N;i++) { scanf("%lf",&a[i]); } double l=-1e6,r=1e6; while(r-l>eps) { double mid=(r+l)/2; for(int i=1;i<=N;i++) b[i]=a[i]-mid; for(int i=1;i<=N;i++) { sum[i]=sum[i-1]+b[i]; } double ans=-1e10; double min_val=1e10; for(int i=L;i<=N;i++) { min_val=min(min_val,sum[i-L]); ans=max(ans,sum[i]-min_val); } if(ans>=0) l=mid; else r=mid; } printf("%d\n",int(r*1000)); }