标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 277 Accepted Submission(s): 132
/** 题意:如题 做法:dp 比赛的时候想到三维,可是我想的是dp[i][j][k] i表示走的第几步 j,k表示坐标 然后回溯回去,看了题解 由于数据范围不大 座椅可以直接暴力枚举平均值 然后dp[i][j] 表示走i,j 的期望 在计算的时候算的是((n+m-1) * mmap[i][j] - aver) 这样在结尾的时候除以(n+m-1) 可是防止精度的问题 **/ #include <iostream> #include <cmath> #include <algorithm> #include <stdio.h> #include <string.h> using namespace std; #define maxn 35 #define INF 0x7fffffff int mmap[maxn][maxn]; int dp[maxn][maxn]; int n, m; long long solve(int x) { memset(dp, 0, sizeof(dp)); int tt = n + m - 1; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(i == 1 && j == 1) { dp[i][j] = (tt * mmap[1][1] - x) * (tt * mmap[1][1] - x); } else if(i == 1) { dp[i][j] = dp[i][j - 1] + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x); } else if(j == 1) { dp[i][j] = dp[i - 1][j] + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x); } else { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x); } } } return dp[n][m] / tt; } int main() { int T; scanf("%d", &T); int Case = 1; while(T--) { scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &mmap[i][j]); } } long long ans = INF; for(int i = 1; i <= 2000; i++) { ans = min(ans, solve(i)); } printf("Case #%d: %lld\n", Case++, ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4843173.html