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
样例输入
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
题目大意:
给一个R*C的矩阵,先横着切A-1刀,分成A份,然后每份切B-1刀,最终有A*B份,求里面的最小值的最大值。
二分枚举答案,然后判断是否可行。
#include <bits/stdc++.h>
using namespace std;
int r,c,a,b;
int s[][],pre[][];
bool check(int x)
{
int cnt=,la=;
for(int i=;i<=r;i++)
{
int sum=,num=;
for(int j=;j<=c;j++)
{
if(sum+pre[i][j]-pre[i][j-]-pre[la][j]+pre[la][j-]<x)
sum+=pre[i][j]-pre[i][j-]-pre[la][j]+pre[la][j-];
else
sum=,num++;
}
if(num>=b)///说明这部分能被切除b份,且符合要求
la=i,cnt++;///更新上一次切的行的位置
}
return cnt>=a;
}
int main()
{ scanf("%d%d%d%d",&r,&c,&a,&b);
for(int i=;i<=r;i++)
for(int j=;j<=c;j++)
scanf("%d",&s[i][j]);
for(int i=;i<=r;i++)///处理前缀和
for(int j=;j<=c;j++)
pre[i][j]=pre[i-][j]+pre[i][j-]-pre[i-][j-]+s[i][j];
int L=,R=pre[r][c],ans;
while(L<=R)
{
//cout<<L<<' '<<R<<'\n';
int mid=(L+R)>>;
if(check(mid))
L=mid+,ans=mid;
else R=mid-;
}
printf("%d\n",ans);
return ;
}
Brownie Slicing(二分枚举答案)的更多相关文章
- BZOJ 2196: [Usaco2011 Mar]Brownie Slicing( 二分答案 )
二分答案就可以了.... ----------------------------------------------------------------------- #include<cst ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ3228 并查集或二分最大流枚举答案
忘记写题意了.这题题意:给出每个地点的金矿与金库的数量,再给出边的长度.求取最大可通过边长的最小权值使每个金矿都能运输到金库里. 这题和之前做的两道二分枚举最大流答案的问法很相识,但是这里用最大流速度 ...
- BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心
BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...
- NC24622 Brownie Slicing
NC24622 Brownie Slicing 题目 题目描述 Bessie has baked a rectangular brownie that can be thought of as an ...
- 两条直线(蓝桥杯)二分枚举+RMQ
算法提高 两条直线 时间限制:1.0s 内存限制:256.0MB 问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...
- 无题II hdu 2236(二分枚举区间)
分析:只需要用二分找一个区间,然后不断枚举这个区间是否可以达到最大匹配,一直二分到答案为止. 代码: =============================================== ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
随机推荐
- JSP文件上传,好烦啊、、
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- php接受axios数据
var params = { username: 'admin', password: '123456' } axios.post('test.php', params).then(res => ...
- MIPS—冒泡排序
SORT 使用冒泡排序对整数数组进行排序,这种排序虽然不是最快的,但却是最简单的. C语言代码 #include<stdio.h> #include<iostream> usi ...
- 单源最短路SPFA
#include<iostream> #include<queue> #include<cstring> #define INF 0x3f3f3f3f using ...
- sql视图和表的区别
整理一下视图和表的区别 区别: 1.视图是已经编译好了的sql,表不是 2.视图没有实际的物理存储记录,表有 3.视图是逻辑概念,表可以进行修改 5.表是内模式,视图是外模式 6.视图是我们查看表的方 ...
- python matplotlib.pyplot对图像进行绘制
imshow()是对图像进行绘制 imshow()函数格式为: matplotlib.pyplot.imshow(X, cmap=None) X: 要绘制的图像或数组. cmap: 颜色图谱(colo ...
- Mathematics-基础:散列函数
一,概念: 散列(HASH)函数H也称哈希函数.是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位.160位,比输入的串短),该串h被称为输入x的Hash值. ...
- shell脚本,计算创建100个文件所用多少时间。
[root@localhost mulu]# ls [root@localhost mulu]# `; do touch file$i; done real 0m0.104s user 0m0.012 ...
- ios之自定义UINavigationBar
ios5 自定义导航条问题 在ios5之前的系统中,可以通过定义导航条类别的方式自定义导航条: @implementation UINavigationBar (CustomImage)- (void ...
- C# Excel常用控件总结
参考:https://blog.csdn.net/waterstar50/article/details/80590355 1.ClosedXML2.EPPlus 教程:http://www.cnbl ...