HNACM(八)G- Interference Signal

传送门

动态规划问题
dp[i][j] 表示从i到j所有数的平均值
状态转移公式是dp[i][j] = (dp[i][j-1]*(j-i)+num[j])/(j-i+1);

#include <bits/stdc++.h> 
#define N 2010
#define ll long long
#define MAX 11111
using namespace std;
int num[N];
double dp[N][N];
int main(){
//#ifndef ONLINE_JUDGE
// freopen("1.txt", "r", stdin);
//#endif 
    int i, j, k, n, m; 
    double ans;
    scanf("%d", &k);
    while(k--){
        scanf("%d%d", &n, &m);
        ans = 0;
        memset(dp, 0, sizeof(dp));
        for (i = 1; i <= n; i++){
            scanf("%d", &num[i]);
            dp[i][i] = num[i]*1.0;
        }
        ans = 0;
        for (i = 1; i < n; i++){
            for (j = i+1; j <= n; j++){
                dp[i][j] = (dp[i][j-1]*(j-i)+num[j])/(j-i+1);
                if (j-i+1 >= m){
                    ans = max(ans, dp[i][j]);
                }
            }
        }
        ans *= 1000;
        printf("%d\n", (int)ans);
    }
    return 0;
}

你可能感兴趣的:(HNACM(八)G- Interference Signal)