Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

记忆化搜索也能 0ms AC,顺便总结下这题wa遇到的几个坑(附代码)

Posted by On18 at 2020-04-17 12:40:59 on Problem 1191
1.你设置的最小值应当大于40960000,因为一个分块最大平方和是(64*100)^2
2.分块的时候竖切选左面和选右面是不同情况,横切同理
3.code
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int board[9][9];
double x_bar;
double dp[9][9][9][9][15];

inline double score(int a, int b, int c, int d)
{
    double ans = 0;
    for (int i = a; i <= b; ++i)
        for (int j = c; j <= d; ++j)
            ans += board[i][j];
    return ans;
}

double dfs(int a, int b, int c, int d, int n)
{
    if (dp[a][b][c][d][n] != 0)
        return dp[a][b][c][d][n];
    if (n == 1)
    {
        double t = score(a, b, c, d) - x_bar;
        dp[a][b][c][d][1] = t * t;
        return t * t;
    }
    double ans = 99999999;
    double t;
    for (int i = a + 1; i <= b; ++i)
    {
        t = score(a, i - 1, c, d) - x_bar;
        ans = min(ans, dfs(i, b, c, d, n - 1) + t * t);
        t = score(i, b, c, d) - x_bar;
        ans = min(ans, dfs(a, i - 1, c, d, n - 1) + t * t);
    }
    for (int i = c + 1; i <= d; ++i)
    {
        t = score(a, b, c, i - 1) - x_bar;
        ans = min(ans, dfs(a, b, i, d, n - 1) + t * t);
        t = score(a, b, i, d) - x_bar;
        ans = min(ans, dfs(a, b, c, i - 1, n - 1) + t * t);
    }
    dp[a][b][c][d][n] = ans;
    return ans;
}

int main()
{
    int n;
    cin >> n;
    double sum = 0;
    for (int i = 1; i <= 8; ++i)
        for (int j = 1; j <= 8; ++j)
        {
            cin >> board[i][j];
            sum += board[i][j];
        }
    x_bar = sum / n;
    double ans = dfs(1, 8, 1, 8, n);
    ans = sqrt(ans / n);
    printf("%.3f", ans);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator