描述
Given an N x N square grid (2 <= N <= 15) and each grid has some beans in it. You want to write at most K (1 <= K <= 2N - 2) horizontal or vertical lines going across the entire grid, which to partition the grid into some piles.
But you want to minimize the number of the largest resulting piles of beans. Given the number of beans in each square, please compute the
number of the largest pile of beans.
输入
The first line has two integers, N and K and then follows N lines, each line has N non-negative integers, indicating the number of beans no more than 1000.
输出
Output the minimum possible number of the largest pile of beans.
样例输入
3 2
1 1 2
1 1 2
2 2 4
样例输出
4
提示
write vertical line between columns 2 and 3, and another horizontal line between rows 2 and 3, which creates 4 piles, each with 4 beans.
题意:
给一个n*n的矩阵,横着或竖着切最多k次,最小化切完后所有块数值和的最大值。
题目分析:
可以枚举横着切的情况,然后二分答案,用贪心判可不可行。
#include <bits/stdc++.h> using namespace std; const int N=20; int a[N][N],t[N],s[N],cnt; int n,k,sum; bool check(int x) { for(int i=0;i<cnt;i++) s[i]=0; int ans=0; for(int i=1;i<=n;i++) { bool f=true; for(int j=1;j<cnt;j++) { s[j]+=a[t[j]][i]-a[t[j-1]][i]; if(s[j]>x) f=false; } if(f) continue; ans++; for(int j=1;j<cnt;j++) { s[j]=a[t[j]][i]-a[t[j-1]][i]; if(s[j]>x) return false; } } return ans+cnt-2<=k; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&a[i][j]); sum+=a[i][j]; a[i][j]+=a[i-1][j]; } } int S=1<<(n-1),ans=1e9; for(int s=0;s<S;s++) {//枚举横切情况 cnt=0; t[cnt++]=0; for(int i=0;i<n-1;i++) { if((1<<i)&s) t[cnt++]=i+1; } t[cnt++]=n; if(cnt-2>k) continue; int L=0,R=sum,x; while(L<=R) { int mid=(L+R)>>1; if(check(mid)) x=mid,R=mid-1; else L=mid+1; } ans=min(ans,x); } printf("%d ",ans); return 0; }