标签:john long sts and ble enter register body 前缀和
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16945 | Accepted: 5425 |
Description
Input
Output
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)); }
POJ-2018 Authors Register Update your info Authors ranklist
标签:john long sts and ble enter register body 前缀和
原文地址:https://www.cnblogs.com/cautx/p/11885624.html