zoukankan      html  css  js  c++  java
  • 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<<'
    ';
            int mid=(L+R)>>1;
            if(check(mid))
                L=mid+1,ans=mid;
            else R=mid-1;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    扩展AuthorizeAttribute
    扩展ValidationAttribute 1
    动态linq to list排序
    CSS属性书写顺序及命名规则
    增强网站可访问性的25种方法
    -webkit-filter是神马?
    docker安装
    docker的体系结构
    docker和虚拟化
    初识docker——docker基本概述
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/9498963.html
Copyright © 2011-2022 走看看