zoukankan      html  css  js  c++  java
  • HDU 2933 MAX Average Problem

    HDU_2933

        具体的思路可以参考《浅谈数形结合思想在信息学竞赛中的应用》

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXD 100010
    typedef long long LL;
    int N, K, A[MAXD], q[MAXD];
    void cin(int &x)
    {
        char ch;
        while(ch = getchar(), ch < '0' || ch > '9');
        x = ch - '0';
        while(ch = getchar(), ch >= '0' && ch <= '9')
            x = (x << 3) + (x << 1) + ch - '0';    
    }
    void init()
    {
        int i;
        A[0] = 0;
        for(i = 1; i <= N; i ++) cin(A[i]), A[i] += A[i - 1];    
    }
    void solve()
    {
        int i, j, x, y, z, front, rear;
        double ans = 0;
        front = rear = 0;
        q[rear ++] = 0;
        for(i = K; i <= N; i ++)
        {
            while(front < rear - 1)
            {
                x = q[front], y = q[front + 1];
                if((LL)(A[i] - A[x]) * (i - y) > (LL)(A[i] - A[y]) * (i - x)) break;
                ++ front;
            }
            ans = std::max(ans, (double)(A[i] - A[q[front]]) / (i - q[front]));
            q[rear] = i - K + 1;
            while(front < rear - 1)
            {
                x = q[rear - 2], y = q[rear - 1], z = q[rear];
                if((LL)(A[z] - A[y]) * (y - x) > (LL)(A[y] - A[x]) * (z - y)) break;
                -- rear, q[rear] = q[rear + 1];
            }
            ++ rear;
        }
        printf("%.2f\n", ans);
    }
    int main()
    {
        while(scanf("%d%d", &N, &K) == 2)
        {
            init();
            solve();
        }
        return 0;    
    }
  • 相关阅读:
    Smarty数学运算
    双引号里值的嵌入
    Smarty属性
    Smarty函数
    Smarty注释代码
    数据结构实验2——链表
    数据结构实验1——顺序表
    hdu 5459 Jesus Is Here
    hdu 5455 Fang Fang
    2018 多校6 hdu 6362 6370 6373
  • 原文地址:https://www.cnblogs.com/staginner/p/2668842.html
Copyright © 2011-2022 走看看