POJ1991 NOI1999棋盘分割
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 15581 | Accepted: 5534 |
Description

原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。
均方差
请编程对给出的棋盘及n,求出O'的最小值。
Input
第2行至第9行每行为8个小于100的非负整数,表示棋盘上相应格子的分值。每行相邻两数之间用一个空格分隔。
Output
Sample Input
3
1 1 1 1 1 1 1 3
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 3
Sample Output
1.633
Source
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define dist(x1, y1, x2, y2) (g[(x2)][(y2)] - g[((x1) - 1)][(y2)] - g[(x2)][((y1) - 1)] + g[((x1) - 1)][((y1) - 1)]) inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long MAXN = + ; long long n, g[][], dp[MAXN][][][][], sum; int main()
{
read(n);
for(register long long i = ;i <= ;++ i)
for(register long long j = ;j <= ;++ j)
{
read(g[i][j]);
sum += g[i][j];
g[i][j] = g[i - ][j] + g[i][j - ] - g[i - ][j - ] + g[i][j];
}
memset(dp, 0x3f, sizeof(dp));
//dp[i][x1][y1][x2][y2]表示把(x1,y1)(x2,y2)矩形切割成i块的最小平方和
for(register long long i = ;i <= n;++ i)
for(register long long x1 = ;x1 <= ;++ x1)
for(register long long y1 = ;y1 <= ;++ y1)
for(register long long x2 = ;x2 <= ;++ x2)
for(register long long y2 = ;y2 <= ;++ y2)
{
if(i == )
{
dp[i][x1][y1][x2][y2] = dist(x1, y1, x2, y2)*dist(x1, y1, x2, y2);
continue;
}
for(register long long a = x1;a < x2;++ a)
{
dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - ][x1][y1][a][y2] + dist(a + , y1, x2, y2)*dist(a + , y1, x2, y2));
dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - ][a + ][y1][x2][y2] + dist(x1, y1, a, y2)*dist(x1, y1, a, y2));
}
for(register long long a = y1;a < y2;++ a)
{
dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - ][x1][y1][x2][a] + dist(x1, a + , x2, y2)*dist(x1, a + , x2, y2));
dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - ][x1][a + ][x2][y2] + dist(x1, y1, x2, a)*dist(x1, y1, x2, a));
}
}
printf("%.3lf", (double)sqrt(((double)dp[n][][][][]*1.0/n) - ((double)sum*1.0/n) * ((double)sum*1.0/n)));
return ;
}
POJ1991
POJ1991 NOI1999棋盘分割的更多相关文章
- [NOI1999] 棋盘分割
COGS 100. [NOI1999] 棋盘分割 http://www.cogs.pro/cogs/problem/problem.php?pid=100 ★★ 输入文件:division.in ...
- [NOI1999] 棋盘分割(推式子+dp)
http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 156 ...
- POJ 1191 棋盘分割
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11213 Accepted: 3951 Description 将一个 ...
- poj 1191 棋盘分割 动态规划
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11457 Accepted: 4032 Description ...
- NOI 193棋盘分割.cpp
193:棋盘分割 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- POJ 1191棋盘分割问题
棋盘分割问题 题目大意,将一个棋盘分割成k-1个矩形,每个矩形都对应一个权值,让所有的权值最小求分法 很像区间DP,但是也不能说就是 我们只要想好了一个怎么变成两个,剩下的就好了,但是怎么变,就是变化 ...
- 洛谷 P1436 棋盘分割 解题报告
P1436 棋盘分割 题目描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的两部分中的任意一块继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共 ...
- poj1191 棋盘分割【区间DP】【记忆化搜索】
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16263 Accepted: 5812 Description ...
随机推荐
- CSS3如何实现圆圈转圈,附demo
如图,如何实现圆圈转圈? 主要还是CSS3动画(只兼容了谷歌,需要兼容其它浏览器,加前缀即可) .move1 { animation: myMove1 5s ease-in infinite alte ...
- Spring中的Junit
Spring中的Junit package com.imooc.test.base; import org.junit.After; import org.junit.Before; import o ...
- 34 N皇后问题Ⅱ
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens-ii/ 34. N皇后问题 II 描述 笔记 数据 评测 讨论区 根据n皇后问题, ...
- 一起使用Python里for循环和dictionary字典
1.先定义一个字典的内容 i= { 'status': 'success', 'country': '中国', 'countryCode': 'CN', 'region': 'BJ' } 2.打印字典 ...
- vue 监听返回
mounted: function() { //使用keep-alive时可以放在activated内 if (window.history && window.history.pus ...
- Linux-c glib库hash表GHashTable介绍
百度云glib 链接:https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ 密码:ol6y hash表是一种提供key-value访问的数据结构,通过指定的 ...
- Linux下使用SSH命令行传输文件到远程服务器
目标:CentOS 7 调整 home分区 扩大 root分区 总体过程: 把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home ,恢复/home内容 ...
- 模板:KD-Tree
KD-Tree,用来解决多维空间中的问题,其实就是优化暴力(逃 一般cdq能做的它都能做,而且...既然是优化暴力,那就学习一下了 对与几个n维点,我们将它每一维分割,建立一颗二叉树,方便我们搜索剪枝 ...
- 深入浅出 Java Concurrency (5): 原子操作 part 4[转]
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁(后面的章节还会谈到锁). 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度 ...
- PAT甲级——A1065 A+B and C (64bit)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...