码迷,mamicode.com
首页 > 其他好文 > 详细

Brownie Slicing(二分枚举答案)

时间:2018-08-18 23:45:10      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:i+1   位置   hoc   最大值   矩阵   each   VID   ike   his   

描述

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;
}

 

Brownie Slicing(二分枚举答案)

标签:i+1   位置   hoc   最大值   矩阵   each   VID   ike   his   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!