BNUOJ 3958 MAX Average Problem

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

2009 Multi-University Training Contest 19 - Host by BNU

Author

[email protected]

解题:单调队列+斜率优化

 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 }

时间: 2024-12-27 23:54:01

BNUOJ 3958 MAX Average Problem的相关文章

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

MAX Average Problem(斜率优化dp)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7574    Accepted Submission(s): 1667 Problem Description Consider a simple sequence which only contains positive integers as

hdu 2993 MAX Average Problem(斜率DP入门题)

题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训队论文<周源--浅谈数形结合思想在信息学竞赛中的应用> 这题输入有点大,要加读入优化才能过. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5

HDU 2993 MAX Average Problem(斜率优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Problem Description 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

HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的平均值最小. 解题思路:斜率DP经典题, 详细分析见: NOI2004年周源的论文<浅谈数形结合思想在信息学竞赛中的应用> 还有要注意要用输入输出外挂,不是getchar()版的,是fread()版的,第一次遇到这么变态的题目- -|||. 代码: 1 #include<iostream&g

【斜率优化】HDU 2993 MAX Average Problem

通道 题意:求出长度大于k子序列使得其各个元素之和的平均数最大,并输出最大平均值 思路:浅谈数形结合思想在信息学竞赛中的应用 代码: #include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int MAXN=100010; int sum[MAXN]; int q[M

BNUOJ 5227 Max Sum

Max Sum Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 100364-bit integer IO format: %I64d      Java class name: Main Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-

[Algorithm] Max Chars Problem

// --- Directions // Given a string, return the character that is most // commonly used in the string. // --- Examples // maxChar("abcccccccd") === "c" // maxChar("apple 1231111") === "1" function maxChar(str) { let

ACM总结——dp专辑(转)

感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ***********************