zoukankan      html  css  js  c++  java
  • HDU2993_MAX Average Problem

    题目要求你在n个数的序列中,找出一段连续的长度不小于k的连续的序列,使得这个序列的平均数最大。输出这个平均数。

    典型的优先队列。首先我们需要根据输入的序列,制造一个和序列。

    然后从k开始往后面走,其实每走一步都维护了一下优先队列,然后加入当前的这个数。

    很简单吧应该。。。。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define maxn 100100
     5 using namespace std;
     6 
     7 int a[maxn],tep,n,k,tot,sum[maxn];
     8 int head,tail,j;
     9 int q[maxn];
    10 double ans,tmp,dx1,dx2,dy1,dy2;
    11 char c;
    12 
    13 int getnum()
    14 {
    15     tot=0;
    16     c=getchar();
    17     while (c<'0' || c>'9') c=getchar();
    18     while (c>='0' && c<='9') tot=tot*10+c-'0',c=getchar();
    19     return tot;
    20 }
    21 
    22 int main()
    23 {
    24
    25     while (scanf("%d%d",&n,&k)!=EOF)
    26     {
    27         tep=0;
    28         for (int i=1; i<=n; i++) a[i]=getnum(),tep+=a[i],sum[i]=tep;
    29         head=1,tail=0;
    30         ans=0;
    31         for (int i=k; i<=n; i++)
    32         {
    33             j=i-k;
    34             while (tail-head>=1)
    35             {
    36                 dx1=q[tail]-q[tail-1];
    37                 dy1=sum[q[tail]]-sum[q[tail-1]];
    38                 dx2=j-q[tail];
    39                 dy2=sum[j]-sum[q[tail]];
    40                 if (dy2*dx1<=dy1*dx2) tail--;
    41                     else break;
    42             }
    43             q[++tail]=j;
    44             while (tail-head>=1)
    45             {
    46                 dx1=i-q[head];
    47                 dy1=sum[i]-sum[q[head]];
    48                 dx2=i-q[head+1];
    49                 dy2=sum[i]-sum[q[head+1]];
    50                 if (dy2*dx1>=dy1*dx2) head++;
    51                     else break;
    52             }
    53             tmp=(sum[i]-sum[q[head]])*1.0/(i-q[head]);
    54             ans=max(ans,tmp);
    55         }
    56         printf("%.2f
    ",ans);
    57     }
    58     return 0;
    59 }
    如有转载,请注明出处(http://www.cnblogs.com/lochan)
  • 相关阅读:
    最大流问题
    字符串的回文与镜像
    字符串的回文与镜像
    Aho
    linux环境变量
    【Centos7】安装memcached
    linux命令后台执行
    ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决
    ubuntu server解决不能访问外网问题
    【Ubuntu 16】安装net-snmp
  • 原文地址:https://www.cnblogs.com/lochan/p/3420851.html
Copyright © 2011-2022 走看看