当前位置:网站首页>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;
}
边栏推荐
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- Ks006 student achievement management system based on SSM
- uTools
- Altium designer measure distance (ctrl+m)
- Six lessons to be learned for the successful implementation of edge coding
- Brief introduction to the development of mobile network
- Introduction to ffmpeg Lib
- Parted command
- Learn about servlets
- SQLite 3 of embedded database
猜你喜欢

Exclusive delivery of secret script move disassembly (the first time)

Learning note 24 - multi sensor post fusion technology

Raspberry pie 4B learning notes - IO communication (1-wire)
![[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility](/img/8b/e51867cfe9d200ac385e1d1f01e4b3.jpg)
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)

matlab 使用 resample 完成重采样

卷積神經網絡(包含代碼與相應圖解)

6-2漏洞利用-ftp不可避免的问题
![[rust web rokcet Series 2] connect the database and add, delete, modify and check curd](/img/23/cfa13ad30e45ef7cdda690775964a7.jpg)
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
随机推荐
SQLite 3 of embedded database
Study note 2 -- definition and value of high-precision map
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
机器学习基本概念
No converter found for return value of type: class
Is the knowledge of University useless and outdated?
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
遊戲思考15:全區全服和分區分服的思考
Liteos learning - first knowledge of development environment
Using tabbar in wechat applet
np. Where and torch Where usage
Experimental reproduction of variable image compression with a scale hyperprior
Learning note 3 -- Key Technologies of high-precision map (Part 1)
Six lessons to be learned for the successful implementation of edge coding
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production
ECS project deployment
Automatically browse pinduoduo products
Laravel artisan common commands
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation