poj 2018 分类: poj 2015-04-22 13:48 32人阅读 评论(0) 收藏


题意:在所有长度大于等于m的区间中找一个平均值最大的,输出平均数。

solution:DP

f(i)num(i) 分别表示最后取第 i 个元素的最优决策 元素值之和元素个数

对于 f(i),只有两种情况:
1. f(i)=nj=nm+1cow(j),num(i)=m
2. f(i)=f(i1)+cow(i),num(i)=num(i1)+1


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>

const int MAXN = 1e5+5;

int n, m;
int c[MAXN] = {0}, sum[MAXN] = {0};
int f[MAXN] = {0}, num[MAXN] = {0};
int ans = 0;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("poj2018.in","r",stdin);
    freopen("poj2018.out","w",stdout);
#endif

    std::cin >> n >> m;

    for(int i = 1; i <= n; i++) 
      {scanf("%d",c+i); sum[i] = sum[i-1]+c[i];}

    f[m] = sum[m], num[m] = m;
    for(int i = m+1; i <= n; i++)
    {
        f[i] = sum[i] - sum[i-m] ,num[i] = m;

        if((f[i-1]+c[i])*num[i] > f[i]*(num[i-1]+1) )
            f[i] = f[i-1]+c[i], num[i] = num[i-1]+1;    
    }

    for(int i = m; i <= n; i ++)
        ans = std::max(ans, f[i]*1000/num[i]);

    std::cout << ans << std::endl;  

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif  
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-22 13:48  <Dash>  阅读(119)  评论(0编辑  收藏  举报