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[505][505],pre[505][505];
bool check(int x)
{
    int cnt=0,la=0;
    for(int i=1;i<=r;i++)
    {
        int sum=0,num=0;
        for(int j=1;j<=c;j++)
        {
            if(sum+pre[i][j]-pre[i][j-1]-pre[la][j]+pre[la][j-1]<x)
                sum+=pre[i][j]-pre[i][j-1]-pre[la][j]+pre[la][j-1];
            else
                sum=0,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=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            scanf("%d",&s[i][j]);
    for(int i=1;i<=r;i++)///处理前缀和
        for(int j=1;j<=c;j++)
            pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+s[i][j];
    int L=0,R=pre[r][c],ans;
    while(L<=R)
    {
        //cout<<L<<‘ ‘<<R<<‘\n‘;
        int mid=(L+R)>>1;
        if(check(mid))
            L=mid+1,ans=mid;
        else R=mid-1;
    }
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/zdragon1104/p/9498963.html

时间: 2024-10-09 06:31:18

Brownie Slicing(二分枚举答案)的相关文章

用二分枚举答案题

以二分算法枚举答案的方法非常高效,枚举所需的时间复杂度只需O(logN) 设x为答案,check(x)只有真假两种取值. 能够使用二分枚举算法的条件: 存在一个X,当x小于X时和x大于X时,check(x)真假值不一样. 二分枚举即能够找出真假之间的边界,这个就是所求在满足题意下的最值了. 如果x是浮点数,那么边界的取值注意精度即可. const double eps=1e-7; while(l+eps<r) { mid=(l+r)/2; if(check(mid)) l=mid; else r

POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

题目链接:POJ 2391 Ombrophobic Bovines Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15006   Accepted: 3278 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes

POJ 3273 Monthly Expense (二分枚举)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15822   Accepted: 6321 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and r

hdu 3277 Marriage Match III【最大流+并查集+二分枚举】

Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1491    Accepted Submission(s): 440 Problem Description Presumably, you all have known the question of stable marriage match.

【noi 2.2_7891】一元三次方程求解(二分枚举)

对于noi上的题有2中解法: 1.数据很小(N=100),可以直接打for循环枚举和判断. 2.不会“三分”,便用二分.利用“两根相差>=1”和 f(x1)*f(x2)<0,转换意思为[x,x+1]内不会包含两个根,这样枚举可以保证不漏解.因此,枚举一个个根所在的区间,再用二分枚举找出根.其中,若N=10^5,由于保留2位小数,最好精确到4位小数计算.时间复杂度 O(N)=10^5+3*log(10^4),约为10^5. 以下附上二分的代码—— 1 //20160908 Ann 2 #incl