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 ...
随机推荐
- 为什么HttpContextAccessor要这么设计?
前言 周五在群里面有小伙伴问,ASP.NET Core这个HttpContextAccessor为什么改成了这个样子? 在印象中,这已经是第三次遇到有小伙伴问这个问题了,特意来写一篇记录,来回答一下这 ...
- 数据结构篇(1) ts实现栈的基本操作和解决相关问题
interface Stack { _items: any push(element: any): void pop(): any top(): any size(): any isEmpty(): ...
- windows下的操作
1.java -jar启动war包 将打好的war包丢到tomcat的webapps目录,然后进入tomcat的bin目录双击运行startup.bat会自动解压war包,在浏览器直接可访问web项目
- 前端 pickerview 的效果 实现 省市区 三级联动
效果图 需要引入 大佬写的js 以及 css 源文件里面有大佬的地址 这是我存在gitee上的文件 https://gitee.com/depressiom/address-pickview-effe ...
- XCTF练习题---WEB---weak_auth
XCTF练习题---WEB---weak_auth flag:cyberpeace{a9aa5a05f5957a19643640028dbb6946} 解题步骤: 1.观察题目,打开场景 2.观察场景 ...
- Gitlab-runner+Docker自动部署SpringBoot项目
本文基于Gitlab CI/CD及Docker快速实现项目的自动部署. 注意:本文较长,浏览需要12分钟左右. 1.环境要求 以下服务器的操作系统均为Centos7 服务器A:Gitlab 服务器B: ...
- Linux主流发行版本配置IP总结(Ubuntu、CentOS、Redhat、Suse)
我们先了解下IP的概念 IP地址简介 电脑连接互联网的必要条件:IP地址+子网掩码+网关+DNS IP地址是上网的唯一标识 - IPv4地址分类: IPv4地址分为A-E共计5类地址,其中A.B.C是 ...
- 【翻译】ScyllaDB数据建模的最佳实践
文章翻译自Scylla官方文档:https://www.scylladb.com/2019/08/20/best-practices-for-data-modeling/ 转载请注明出处:https: ...
- 5┃音视频直播系统之 WebRTC 中的协议UDP、TCP、RTP、RTCP详解
一.UDP/TCP 如果让你自己开发一套实时互动直播系统,在选择网络传输协议时,你会选择使用UDP协议还是TCP协议 假如使用 TCP 会怎样呢?在极端网络情况下,TCP 为了传输的可靠性,将会进行反 ...
- 项目中导入本地jar包问题
1. 问题 一个Maven项目,需要依赖一个本地jar包,以如下方式引用: <dependency> <groupId>xxx.sdk</groupId> < ...