• 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

    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
    ",ret);
    26     }
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    svn命令
    Lambda 表达式
    JAVA 探究NIO
    JAVA I/O系统
    MySQL 锁
    spring boot 集成 zookeeper 搭建微服务架构
    架构演化
    JAVA 集合
    spring boot 发送邮件
    MySQL 隔离级别
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4803572.html
Copyright © 2020-2023  润新知