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;    
}

 

 

相关文章:

  • 2021-12-28
  • 2021-07-14
  • 2022-12-23
  • 2021-11-20
  • 2021-12-14
  • 2021-10-04
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2022-01-08
  • 2022-12-23
  • 2021-07-31
  • 2022-01-15
相关资源
相似解决方案