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 ...
随机推荐
- IP协议/地址(IPv4&IPv6)概要
IP协议/地址(IPv4&IPv6)概要 IP协议 什么是IP协议 IP是Internet Protocol(网际互连协议)的缩写,是TCP/IP体系中的网络层协议. [1] 协议的特征 无连 ...
- 使用 VS Code 撰写 Markdown 文档
众所周知, VS Code 是微软和社区一起开发的一款很优秀的高级代码编辑器.它不仅可以写出一手好代码,还能写出一篇好文章.利用 Markdown 就可以写出一篇排版美观的技术文章了. 而 Markd ...
- RecyclerView + SQLite 简易备忘录-----中(2)
(3)RecyclerView的实现 ---中间的内容 RecyclerView是一个比ListView更加强大的滚动控件.要使用这个控件需要先在项目的build.gradle中添加RecyclerV ...
- Elemnt ui 组件封装(form)
<template> <el-form class="form" :inline="formConfig.inline" :model=&qu ...
- Windows下搭建redis 哨兵环境
从 https://github.com/tporadowski/redis/releases 下载windows版的redis,自行下载解压. 关于哨兵模式的讲解,强烈推荐 [深入学习redis(4 ...
- DDT数据驱动性能测试(一)
DDT数据驱动性能测试(一) 一.csv数据文件设置 1.使用场景:测试过程中需要使用手机号码等大量数据时,用random函数随机生成数字:也可以使用Excel拖动生成一批手机号,也有可以从数据库中导 ...
- Oracle中查找某个点半径范围内的所有经纬度(优化)
需求: 已知一个点的经纬度,需要从表中找出以这个点为中心,半径M米范围内的所有经纬度数据. 假设现有表 TAB_LONG_LAT_DATA,字段如下: ID INTE ...
- .Net Core Razor动态菜单实现
准备 1.框架 .netcore 版本 yishaadmin开源框架 2.模板 本文模板使用adminlte3.0,文档地址https://adminlte.io/docs/3.0/ 3.菜单表 关 ...
- 浅尝Spring注解开发_Bean生命周期及执行过程
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...
- 拖动元素调换位置——sortable.js
使用简介: https://github.com/SortableJS/Sortable https://segmentfault.com/a/1190000008209715 /**! * Sort ...