首页 > ACM题库 > HDU-杭电 > HDU 2993-MAX Average Problem-动态规划-[解题报告]HOJ
2014
02-24

HDU 2993-MAX Average Problem-动态规划-[解题报告]HOJ

MAX Average Problem

问题描述 :

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.

输入:

There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 … an. All numbers are ranged in [1, 2000].

输出:

There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 … an. All numbers are ranged in [1, 2000].

样例输入:

10 6
6 4 2 10 3 8 5 9 4 1

样例输出:

6.50

给由n(n<=10^4)个数组成的正整数序列,求其长度>=k的子序列的平均值的最大值。

本题O(n^2)的dp算法比较好想,但是n的范围较大,会超时,所以,必须进行优化。这里用到的是斜率优化,《浅谈数形结合思想在信息学竞赛中的应用》这篇论文中有这一题的详细介绍。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 100010;
double a[maxn], sum[maxn];
int n, k;
int q[maxn], head, tail;

void DP()
{
    head = tail = 0;
    double ans = -1;
    for (int i = k; i <= n; ++i) {
        int j = i - k;
        /*维护下凸*/
        while (tail - head >= 2) {
            double x1 = j - q[tail-1];
            double y1 = sum[j] - sum[q[tail-1]];
            double x2 = q[tail-1] - q[tail-2];
            double y2 = sum[q[tail-1]] - sum[q[tail-2]];
            if (x1 * y2 - y1 * x2 >= 0) {
                tail--;
            } else {
                break;
            }
        }
        q[tail++] = j;
        /*寻找最优解并删除无用元素*/
        while (tail - head >= 2) {
            double x1 = i - q[head];
            double y1 = sum[i] - sum[q[head]];
            double x2 = i - q[head+1];
            double y2 = sum[i] - sum[q[head+1]];
            if (x1 * y2 - y1 * x2 >= 0) {
                head++;
            } else {
                break;
            }
        }
        double tmp = (sum[i] - sum[q[head]]) / (i - q[head]);
        ans = max(ans, tmp);
    }
    printf("%.2lf\n", ans);
}

/*读入优化,否则超时*/
inline int GetInt(){
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        ch = getchar();
    }
    int num = 0;
    while (ch >= '0' && ch <= '9'){
        num = num * 10 + ch - '0';
        ch = getchar();
    }
    return num;
}

int main()
{
    while (scanf("%d%d", &n, &k) != EOF) {
        sum[0] = 0;
        for (int i = 1; i <= n; ++i) {
            a[i] = GetInt();
            sum[i] = sum[i-1] + a[i];
        }
        DP(); 
    }
    return 0;
}

解题参考:http://blog.csdn.net/ahfywff/article/details/7916863


  1. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?