惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
博客园_首页
美团技术团队
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
J
Java Code Geeks
T
Tailwind CSS Blog
Jina AI
Jina AI
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

hsfzxjy 的博客

解决 VSCode + CMake + MSVC 编译器信息乱码的问题 使用 3090 部署 1.58bit 动态量化版 DeepSeek R1 671b 如何在 VS Code DevContainer 中配置 HTTP 代理 如何在跳板机背后的服务器上使用 VS Code Remote - Containers Cohesive Digests for Ints and Floats Rust 中的隐匿概念 —— Place(位置) 美术馆 一尺之槌,日取其半,1075日而竭 老生常谈:使用 Cloudflare 自选 IP 加速站点访问 辩义 State、Nation 与 Country 将 Base64 编码的数据快速转换为 Uint8Array 折腾 NPU·第1章 —— 搭建 Level Zero 开发环境 折腾 NPU·第0章 —— Intel NPU 概述与 Level-Zero 新增域名 monad.run CSS 中为特定字符设置不同字体 Arbitary Lifetime Transmutation via Rust Unsoundness Dijkstra 算法的延伸 Manacher 回文计数算法 硬卧 Go Fact: Zero-sized Field at the Rear of a Struct Has Non-zero Size Display *big.Rat Losslessly and Smartly in Golang 代码的仪式 Building Electron From Scratch 中式亲属称谓研究之一:构建半群 Some Notes on Kotlin Coroutines Git sparse-checkout and partial clones for Mega-Repos 辩义“封建” Diving from the CUDA Error 804 into a bug of libnvidia-container Modern Cryptography, GPG and Integration with Git(hub) Move the Root Partition of Ubuntu
UVa10285 Cake Slicing
2014-10-01 · via hsfzxjy 的博客

链接:Link 耗时:1.825s

这道题做的可真够久的:前前后后加起来将近有两个小时,因此当 AC 的那一刻,自己心中还是挺自豪的。

事实上,这是一道复杂一点的区间型动态规划,之所以说“复杂”,是因为它的状态转移是二维的:切蛋糕既可以横切,也可以纵切。由此我想到了分治算法:

假设一个矩形它所需要切的刀数是 f,则 f 可以由组成该矩形的小矩形的 f 值决定。

因此,这个问题具有最优子结构。由于每个状态为一个矩形,因此需要 4 个维度来记录状态(及左上、右下两个顶点)。下面是横切时的状态转移方程,纵切时同理可得:

$$ \begin{aligned} f(up, down, left, right) = &\ min\{f(up, i, left, right) \\ & + f(i, down, left, right) + right - left\},i = up + 1 .. down -1 \end{aligned} $$

{$R-}
const INF = maxint div 5;
var
f: array [0..20, 0..20, 0..20, 0..20] of integer;
cherries: array [1..500, 1..2] of integer;
map: array [0..20, 0..20] of boolean;
n, m, i, k: integer;

function min(x, y: integer): integer; inline;
begin
if x<y then exit(x) else exit(y);
end;

function cherryin(u, d, l, r: integer): integer; inline;
var
i, j: integer;
begin
cherryin := 0;
for i := u+1 to d do
for j := l+1 to r do
if map[i, j] then
begin
inc(cherryin);
if cherryin = 2 then exit;
end;
end;

function dp(u, d, l, r: integer): integer;
var
b: integer;
i: integer;
begin
if f[u, d, l, r] <> -1 then
exit(f[u,d , l, r]);
b := cherryin(u, d, l, r);
if b = 1 then
begin
f[u, d, l, r] := 0;
exit(0);
end;
if b = 0 then
begin
f[u, d, l, r] := INF;
exit(INF);
end;
dp := INF;
for i := u+1 to d-1 do
dp := min(dp, dp(u, i, l, r)+dp(i, d, l, r)+r-l);
for i := l+1 to r-1 do
dp := min(dp, dp(u, d, l, i)+dp(u, d, i, r)+d-u);
f[u, d, l, r] := dp;
end;

var
_: integer;

begin
assign(input, 'main.in');reset(input);
assign(output, 'main.out');rewrite(output);
_ := 0;
readln(n, m, k);
while n>0 do
begin
inc(_);
fillchar(map, sizeof(map), 0);
fillchar(f, sizeof(f), -1);
for i := 1 to k do
begin
readln(cherries[i, 1], cherries[i, 2]);
map[cherries[i, 1], cherries[i, 2]] := true;
end;
writeln('Case ',_,': ', dp(0,n,0,m));
readln(n, m, k);
end;
close(input);close(output);
end.

作者:hsfzxjy
链接:
许可:CC BY-NC-ND 4.0.
著作权归作者所有。本文不允许被用作商业用途,非商业转载请注明出处。