蛋糕
gmoj 3918
题目大意:
有一个蛋糕,分成n∗mn*mn∗m个单位,现在横竖各切三刀,使其分成16个矩阵,使价值最小的矩阵价值最大
输出样例
5 5
95998
21945
23451
99798
74083
输入样例
3
数据范围
40%的数据, 4 <= R,C <= 10。
60%的数据, 4 <= R,C <= 20。
100%的数据, 4 <= R,C <= 75。
样例解释
解题思路:
二分答案
然后枚举竖着怎样切,最后判断可不可以切
时间复杂度o(m3nlogSum)o(m^3nlogSum)o(m3nlogSum)
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, l, r, k, mid, num, a[100][100];
char x;
int main()
{scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j){cin>>x;a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + x - 48;//前缀和r += x - 48;}l = 1;while (l < r){num = 0;mid = (l + r + 1) >> 1;for (int c1 = 1; c1 <= m - 3 && num < 4; ++c1)//枚举怎么切for (int c2 = c1 + 1; c2 <= m - 2 && num < 4; ++c2)for (int c3 = c2 + 1; c3 <= m - 1 && num < 4; ++c3){k = 1;num = 0;for (int i = 1; i <= n; ++i)if (a[i][c1] - a[k - 1][c1] >= mid && a[i][c2] - a[k - 1][c2] - a[i][c1] + a[k - 1][c1] >= mid && a[i][c3] - a[k - 1][c3] - a[i][c2] + a[k - 1][c2] >= mid && a[i][m] - a[k - 1][m] - a[i][c3] + a[k - 1][c3] >= mid) //是否每一行的四个矩阵都大于mid了{k = i + 1;//切开num++;}}if (num >= 4) l = mid;//是否满足else r = mid - 1; }printf("%d", l);
}