当前位置:网站首页>321. Chessboard segmentation (2D interval DP)
321. Chessboard segmentation (2D interval DP)
2022-07-02 01:45:00 【seez】

321. Chessboard division - AcWing Question bank
The question :

- A matrix can be crosscut , Or side cutting , As shown in the figure
- For a chessboard cut into two parts , You can and can only select one to continue cutting down
Find the minimum variance , Due to mean square deviation X Fix ,n Fix , What we need to calculate is the area of the divided sub rectangle (xi-X)*(xi-X).
therefore , You can use the search method , Keep raising the minimum
analysis : Two dimensional interval dp, In fact, it is similar to the digital triangle model , Because segmentation may not have a linear recursive relationship , So we use the method of memory search .

- Crosscut i, In two parts [(x1,y1)~(i,y2)],[(i+1,y2)~(x2,y2)]
- Vertical cutting i, In two parts [(x1,y1)~(x2,i)],[(x2,i+1)~(x2,y2)]
initialization : All initialized to infinity , Due to the particularity of planned search , So first initialize to a negative number , Enter to reinitialize
State transition equation / State calculation :
According to the position of cross cutting and vertical cutting i Different , And which subset to continue to divide after division , It can be divided into multiple sets
Crosscut , It is divided into two subsets , Both subsets can be further divided , Another direct calculation
- dp[x1][y1][x2][y2][k]=max(dp[x1][i][x2][y2][k]+sum[x1~i][y1~y2]);
- dp[x1][y1][x2][y2][k]=max(dp[x1][y1][i+1][y2][k]+sum[i+1~]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=16;
const int inf=1e9;
int s[N][N];
double dp[N][N][N][N][N];
double X;
double get(int x1,int y1,int x2,int y2)
{
int a=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
return (a-X)*(a-X);
}
double dfs(int x1,int y1,int x2,int y2,int k)
{
double &v=dp[x1][y1][x2][y2][k];// For simplicity , add v
if(v>=0) return v;
if(k==1) return get(x1,y1,x2,y2);
v=1e9; // For the minimum , Initialize to infinity
for(int i=x1;i<x2;i++)
{// Crosscutting subset
v=min(v,dfs(i+1,y1,x2,y2,k-1)+get(x1,y1,i,y2));
v=min(v,dfs(x1,y1,i,y2,k-1)+get(i+1,y1,x2,y2));
}
for(int i=y1;i<y2;i++)
{// Vertically cut subset
v=min(v,dfs(x1,i+1,x2,y2,k-1)+get(x1,y1,x2,i));
v=min(v,dfs(x1,y1,x2,i,k-1)+get(x1,i+1,x2,y2));
}
return v;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
cin>>s[i][j];
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
s[i][j]+=s[i][j-1]+s[i-1][j]-s[i-1][j-1];
X=(double)s[8][8]/n;
memset(dp,-1,sizeof dp); // Because the integral may be 0, So take -1
double a=dfs(1,1,8,8,n)/n;
printf("%.3lf",sqrt(a));
return 0;
}
边栏推荐
- 跨域?同源?一次搞懂什么是跨域
- What is AQS and its principle
- matlab 使用 resample 完成重采样
- Discussion on the idea of platform construction
- Six lessons to be learned for the successful implementation of edge coding
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- Volume compression, decompression
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- ECS project deployment
- uTools
猜你喜欢

游戏思考15:全区全服和分区分服的思考

Convolutional neural network (including code and corresponding diagram)

matlab 使用 audioread 、 sound 读取和播放 wav 文件

Six lessons to be learned for the successful implementation of edge coding

【LeetCode 43】236. The nearest common ancestor of binary tree
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/ba/dcb276768b1a9cc84099f093677d29.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

matlab 实现语音信号重采样和归一化,并播放比对效果

跨域?同源?一次搞懂什么是跨域

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?

Leetcode, 3 repeatless longest subsequence
随机推荐
如何用一款产品推动「品牌的惊险一跃」?
GL Studio 5 installation and experience
MySQL application day02
What are the skills of spot gold analysis?
Leetcode, 3 repeatless longest subsequence
现货黄金分析的技巧有什么呢?
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
The difference between new and malloc
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
Since I understand the idea of dynamic planning, I have opened the door to a new world
电子协会 C语言 1级 32、计算2的幂
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
6-3漏洞利用-SSH环境搭建
What is AQS and its principle
How to debug apps remotely and online?
Number of palindromes in C language (leetcode)