NC24622 Brownie Slicing
NC24622 Brownie Slicing
题目
题目描述
Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500; 1 <= C <= 500) of little brownie squares. The square at row i, column j contains \(N_{ij}\) (0 <= \(N_{ij}\) <= 4,000) chocolate chips.
Bessie wants to partition the brownie up into A*B chunks (1 <= A <= R; 1 <= B <= C): one for each of the A*B cows. The brownie is cut by first making A-1 horizontal cuts (always along integer
coordinates) to divide the brownie into A strips. Then cut each strip independently with B-1 vertical cuts, also on integer
boundaries. The other A*B-1 cows then each choose a brownie piece, leaving the last chunk for Bessie. Being greedy, they leave Bessie the brownie that has the least number of chocolate chips on it.
Determine the maximum number of chocolate chips Bessie can receive, assuming she cuts the brownies optimally.
As an example, consider a 5 row x 4 column brownie with chips
distributed like this:
1 2 2 1
3 1 1 1
2 0 1 3
1 1 1 1
1 1 1 1
Bessie must partition the brownie into 4 horizontal strips, each with two pieces. Bessie can cut the brownie like this:
1 2 | 2 1
---------
3 | 1 1 1
---------
2 0 1 | 3
---------
1 1 | 1 1
1 1 | 1 1
Thus, when the other greedy cows take their brownie piece, Bessie still gets 3 chocolate chips.
输入描述
- Line 1: Four space-separated integers: R, C, A, and B
- Lines 2..R+1: Line i+1 contains C space-separated integers: \(N_{i1}, ..., N_{iC}\)
输出描述
- Line 1: A single integer: the maximum number of chocolate chips that Bessie guarantee on her brownie
示例1
输入
5 4 4 2
1 2 2 1
3 1 1 1
2 0 1 3
1 1 1 1
1 1 1 1
输出
3
题解
思路
知识点:二分,前缀和。
显然二分所有块的最小值。对于某个答案,只要让分的每块都的大于等于他即可。检查过程如下:
- 因为行列划分不交叉,因此可以考虑先从行起始点 \(prea\) 开始划一个横条,对这一整条进行列划分出每一块。
- 对于每一块,从列起始点 \(preb\) 记录其和 \(sum\),到某一列 \(j\) 时到达答案就停止纳入,将列起始点 \(preb\) 更新到 \(j+1\) ,进行对下一块归纳,并且此横条的可划分块数 \(cntb\) 加一;否则继续纳入这个条的下一列。
- 如果划分到某一行 \(i\) ,这一整条的 \(cntb >= B\) ,则说明这个横条划分合格,将把 \(prea\) 更新为 \(i+1\) ,并把矩阵可划分条数 \(cnta\) 加一;否则不合格,继续纳入下一行。
- 最后如果 \(cnta >= A\) 说明行列划分合格,这答案是可行的;否则不合格。
注意到每次计算一个块的总和是重复的,考虑预处理二位前缀和,能直接计算出这块的大小为:
\]
要注意的是,起始行列都是 \(1\) 不是 \(0\) 注意不要坑到自己qwq。
时间复杂度 \(O(RC)\)
空间复杂度 \(O(RC)\)
代码
#include <bits/stdc++.h>
using namespace std;
int R, C, A, B;
int N[507][507];
bool check(int mid) {
int prea = 1, cnta = 0;///注意下标啊,起始行列都是1不是0
for (int i = 1;i <= R;i++) {
int preb = 1, cntb = 0;
for (int j = 1;j <= C;j++) {
int sum = N[i][j] - N[prea - 1][j] - N[i][preb - 1] + N[prea - 1][preb - 1];
if (sum >= mid) {
preb = j + 1;
cntb++;
}
}
if (cntb >= B) {
prea = i + 1;
cnta++;
}
}
return cnta >= A;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> R >> C >> A >> B;
for (int i = 1;i <= R;i++)
for (int j = 1;j <= C;j++)
cin >> N[i][j], N[i][j] += N[i - 1][j] + N[i][j - 1] - N[i - 1][j - 1];
int l = 0, r = 1e9;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) l = mid + 1;
else r = mid - 1;
}
cout << r << '\n';
return 0;
}
NC24622 Brownie Slicing的更多相关文章
- BZOJ 2196: [Usaco2011 Mar]Brownie Slicing( 二分答案 )
二分答案就可以了.... ----------------------------------------------------------------------- #include<cst ...
- Brownie Slicing(二分枚举答案)
描述 Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500 ...
- Usaco*Brownie Slicing
Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C <= 500)个小的巧克力蛋糕组成的. 第i行,第j列的蛋糕有N_ij(1 <= N_ ...
- 【BZOJ】2196: [Usaco2011 Mar]Brownie Slicing
[题意]给定n*m的数字矩阵,要求横着切A-1刀,对每块再分别竖着切B-1刀,是最小子矩阵最大. [算法]二分+贪心 [题解]还记得提高组2015跳石头吗?这道题做法一致,只不过拓展到二维而已. 二分 ...
- BZOJ2196: [Usaco2011 Mar]Brownie Slicing
n<=500 * m<=500的方阵,先沿横坐标切A-1刀,再把每一块切B-1刀,得到A*B块,求这A*B块的数字之和的最小值的最大值. 最小值最大--二分,然后贪心切.每次扫一行,看这一 ...
- bzoj usaco 金组水题题解(2)
续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj Usaco补完计划(优先级 Gold>Silver>资格赛)
听说KPM初二暑假就补完了啊%%% 先刷Gold再刷Silver(因为目测没那么多时间刷Silver,方便以后TJ2333(雾 按AC数降序刷 ---------------------------- ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- Vue_基础功能循环、计算、绑定、事件处理、组件
1 <!DOCTYPE html> 2 <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xht ...
- 大一/初学者学C语言前必看!!!(建议收藏)
目录 数据类型 常量.变量 数组 字符串.转义字符 选择语句 循环语句 函数 操作符 结构体 指针 神秘的学习资料基地jq.qq.com/?_wv=1027&k=5kWJsY1z 一.数据类 ...
- 论文解读(MERIT)《Multi-Scale Contrastive Siamese Networks for Self-Supervised Graph Representation Learning》
论文信息 论文标题:Multi-Scale Contrastive Siamese Networks for Self-Supervised Graph Representation Learning ...
- python数据可视化-matplotlib入门(7)-从网络加载数据及数据可视化的小总结
除了从文件加载数据,另一个数据源是互联网,互联网每天产生各种不同的数据,可以用各种各样的方式从互联网加载数据. 一.了解 Web API Web 应用编程接口(API)自动请求网站的特定信息,再对这些 ...
- MongoDB 常用运维实践总结
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 一.MongoDB 集群简介 MongoDB是一个基于分布式文件存储的数据库,其目的在于为WE ...
- 如何使用Shell写一个显示目录结构的命令?
公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ 在Linux中使用Shell写一个显示目录结构的命令,快速寻找目录结构. 1.代码 #!/usr/bin/env bash ...
- 详谈:pNFS增强文件系统架构
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 通过 NFS(由服务器.客户机软件和两者之间的协议组成) ...
- 测试覆盖率 之 Cobertura的使用
什么是代码覆盖率? 代码覆盖率是对整个测试过程中被执行的代码的衡量,它能测量源代码中的哪些语句在测试中被执行,哪些语句尚未被执行. 为什么要测量代码覆盖率? 众所周知,测试可以提高软件版本的质量和可预 ...
- 探索 Python/Django 支持分布式多租户数据库,如 Postgres+Citus
在 确定分布策略 中,我们讨论了在多租户用例中使用 Citus 所需的与框架无关的数据库更改. 在这里,我们专门研究如何借助 django-multitenant 库将多租户 Django 应 用程序 ...
- 832. Flipping an Image - LeetCode
Question 832. Flipping an Image Solution 题目大意:将1列与最后n列对换,2列与n-1列对换-然后再将每个元素取反 思路:遍历二维数组的左半边,对每个元素先做对 ...