BZOJ2196: [Usaco2011 Mar]Brownie Slicing

n<=500 * m<=500的方阵,先沿横坐标切A-1刀,再把每一块切B-1刀,得到A*B块,求这A*B块的数字之和的最小值的最大值。

最小值最大--二分,然后贪心切。每次扫一行,看这一行能不能切成满足二分值的B块,如果能就记可行横条块多一,最后看可行横条块能否到A,如不能则继续扫下一行,把没满足条件的这行并起来计算。由于需要把连续几行并起来求子矩阵和,需要预处理一波。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<algorithm>
 5 #include<queue>
 6 //#include<iostream>
 7 using namespace std;
 8 
 9 int n,m,A,B;
10 #define maxn 511
11 int a[maxn][maxn],sum[maxn][maxn];
12 bool check(int x)
13 {
14     int lx=0,cntx=0;
15     for (int i=1;i<=n;i++)
16     {
17         int ly=0,cnty=0;
18         for (int j=1;j<=m;j++)
19             if (sum[i][j]-sum[lx][j]-sum[i][ly]+sum[lx][ly]>=x)
20                 cnty++,ly=j;
21         if (cnty>=B) cntx++,lx=i;
22     }
23     return cntx>=A;
24 }
25 int main()
26 {
27     scanf("%d%d%d%d",&n,&m,&A,&B);
28     int tot=0;
29     for (int i=1;i<=n;i++)
30         for (int j=1;j<=m;j++)
31             scanf("%d",&a[i][j]),tot+=a[i][j];
32     memset(sum,0,sizeof(sum));
33     for (int i=1;i<=n;i++)
34         for (int j=1;j<=m;j++)
35             sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
36     int L=-1,R=tot;
37     while (L<R)
38     {
39         const int mid=(L+R+1)>>1;
40         if (check(mid)) L=mid;
41         else R=mid-1;
42     }
43     printf("%d\n",L);
44     return 0;
45 }
View Code

 

posted @ 2017-09-19 14:12  Blue233333  阅读(268)  评论(0编辑  收藏  举报