码迷,mamicode.com
首页 > 其他好文 > 详细

BNUOJ 3958 MAX Average Problem

时间:2015-09-12 21:48:54      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

MAX Average Problem

Time Limit: 3000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
 

Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.

 

Input

There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=M<=10^5).
The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].

 

Output

For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.

 

Sample Input

10 6
6 4 2 10 3 8 5 9 4 1

Sample Output

6.50

Source

Author

HK@Sphinx
 
解题:单调队列+斜率优化
 
技术分享
 1 #include <stdio.h>
 2 #include <iostream>
 3 using namespace std;
 4 typedef long long LL;
 5 const int maxn = 100005;
 6 int q[maxn],hd,tl,n,k;
 7 LL sum[maxn];
 8 bool check(LL a,LL b,LL c){
 9     return (sum[c] - sum[b])*(b - a) <= (sum[b] - sum[a])*(c - b);
10 }
11 int main(){
12     while(~scanf("%d%d",&n,&k)){
13         for(int i = 1; i <= n; ++i){
14             scanf("%I64d",sum + i);
15             sum[i] += sum[i-1];
16         }
17         double ret = 0;
18         hd = tl = 0;
19         for(int i = k; i <= n; ++i){
20             while(hd + 1 < tl && check(q[tl-2],q[tl-1],i - k)) --tl;
21             q[tl++] = i - k;
22             while(hd + 1 < tl && check(q[hd+1],q[hd],i)) ++hd;
23             ret = max(ret,(sum[i] - sum[q[hd]])*1.0/(i - q[hd]));
24         }
25         printf("%.2f\n",ret);
26     }
27     return 0;
28 }
View Code

 

BNUOJ 3958 MAX Average Problem

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4803572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!