poj 1191

来源:互联网 发布:地理空间数据库 编辑:程序博客网 时间:2024/06/12 22:44

#include <iostream>#include <algorithm>#include <queue>#include <cstring>#include <cstdio>#include <vector>#include <string>#include <iterator>#include <cmath>#include <deque>#include <stack>#include <cctype>#include <iomanip>using namespace std;typedef long long ll;typedef long double ld;const int N = 1100;const int INF = 0xfffffff;const double EPS = 1e-8;const ll MOD = 1e9 + 7;const ld PI = acos (-1.0);#define INFL 0x7fffffffffffffffLL#define met(a, b) memset(a, b, sizeof(a))#define put(a) cout << setiosflags(ios::fixed) << setprecision(a)int dp[16][9][9][9][9], sum[9][9]; //左上角为1 1的矩阵和int cut (int k, int x1, int y1, int x2, int y2);int getsum (int x1, int y1, int x2, int y2);int main (){    int n;    while (cin >> n)    {        int x, cot = 0;        met (dp, -1);        met (sum, 0);        for (int i=1; i<9; i++)            for (int j=1; j<9; j++)            {                cin >> x;                cot += x;                sum[i][j] = x + sum[i][j-1] + sum[i-1][j] - sum[i-1][j-1];            }            double avr = cot * 1.0 / n;            int res = cut (n, 1, 1, 8, 8);            double ans = sqrt (res * 1.0 / n - avr * avr);            printf ("%.3f\n", ans);    }    return 0;}int getsum (int x1, int y1, int x2, int y2){    return sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];}int cut (int k, int x1, int y1, int x2, int y2){    int s1, s2;    if (dp[k][x1][y1][x2][y2] != -1) return dp[k][x1][y1][x2][y2];    if (k == 1)    {        s1 = getsum (x1, y1, x2, y2);        return dp[k][x1][y1][x2][y2] = s1 * s1;    }    int minx = INF, tmp1, tmp2;    for (int x=x1; x<x2; x++)    {        s1 = getsum (x1, y1, x, y2);        s2 = getsum (x+1, y1, x2, y2);        tmp1 = min (cut (k-1, x+1, y1, x2, y2) + s1*s1, cut(k-1, x1, y1, x, y2) + s2*s2);        minx = min (minx, tmp1);    }    for (int y=y1; y<y2; y++)    {        s1 = getsum (x1, y1, x2, y);        s2 = getsum (x1, y+1, x2, y2);        tmp2 = min (cut(k-1, x1, y+1, x2, y2) + s1*s1, cut(k-1, x1, y1, x2, y) + s2*s2);        minx = min (minx, tmp2);    }    return dp[k][x1][y1][x2][y2] = minx;}

《算法艺术与信息学竞赛》的116页有讲解,先将方差公式化简,最终确定只需求切割n-1次后n块矩形中每块矩形的总分的平方的和最小。

设这个平方和为res,最终的答案就为res/n-avrg^2开根号

wa的几个地方提示:

用double   %.3lf 输出  G++ wa  c++ ac

用double %.3f 输出 G++ ac  c++ ac


要是正规比赛 怎么能想到坑在这 虽然不知道为什么 但是先记住了




0 0
原创粉丝点击