中文题目见P3017 [USACO11MAR] Brownie Slicing G - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P3017
题目描述
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
code&&代码
#include<bits/stdc++.h>
using namespace std;
int x[501][501];
int r,c,a,b;
bool check(int num) {
int i=1,j=1,tot2=0;
while (j<=r) {
int s=1,sum=0,tot=0;
while (s<=c) {
for (int k=i; k<=j; k++)
sum+=x[k][s];
if (sum>=num) {
sum=0;
tot++;
}
s++;
}
if (tot>=b) {
i=j+1;
tot2++;
}
j++;
}
if (tot2>=a) return true;
else return false;
}
int main() {
int left=-1,right=0;
cin>>r>>c>>a>>b;
for (int i=1; i<=r; i++){
for (int j=1; j<=c; j++) {
cin>>x[i][j];
right+=x[i][j];
}
}
int mid,st=0,ans=0;
while (left<=right) {
mid=(left+right)/2;
if (check(mid)) {
ans=mid;
left=mid+1;
} else right=mid-1;
}
cout<<ans;
return 0;
}