2014
02-24

# 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

#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) {
} 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;
}

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. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？