Brownie Slicing G

P3017 [USACO11MAR] Brownie Slicing G

题目描述

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

输入 #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

输出 #1

3

代码内容

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll r,c,a,b;
ll m[501][501],s[501][501];

bool find(ll x)
{
    ll now=0,num=0;
    for(ll i=1;i<=r;i++)
    {
        ll dis=0,sum=0;
        for(ll j=1;j<=c;j++)
            if(dis+(s[i][j]-s[i][j-1])-(s[now][j]-s[now][j-1])<x) dis+=(s[i][j]-s[i][j-1])-(s[now][j]-s[now][j-1]);
            else sum++,dis=0;
        if(sum>=b) now=i,num++;

    }
    if(num<a) return 0;
    return 1; 
}

int main()
{
    cin>>r>>c>>a>>b;
    for(ll i=1;i<=r;i++)
        for(ll j=1;j<=c;j++)
            cin>>m[i][j];

    for(ll i=1;i<=r;i++)
        for(ll j=1;j<=c;j++)
            s[i][j]=s[i-1][j]+s[i][j-1]+m[i][j]-s[i-1][j-1];

    ll h=0,t=s[r][c];
    ll ans=0;
    while(h<=t)
    {
        ll mid=(h+t)/2;
        if(find(mid))
        {
            h=mid+1;
            ans=mid;
        }
        else t=mid-1;
    }
    
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值