描述

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=;
int a[N][N],t[N],s[N],cnt;
int n,k,sum;
bool check(int x) {
for(int i=;i<cnt;i++) s[i]=;
int ans=;
for(int i=;i<=n;i++) {
bool f=true;
for(int j=;j<cnt;j++) {
s[j]+=a[t[j]][i]-a[t[j-]][i];
if(s[j]>x) f=false;
}
if(f) continue;
ans++;
for(int j=;j<cnt;j++) {
s[j]=a[t[j]][i]-a[t[j-]][i];
if(s[j]>x) return false;
}
}
return ans+cnt-<=k;
}
int main() {
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) {
for(int j=;j<=n;j++) {
scanf("%d",&a[i][j]);
sum+=a[i][j];
a[i][j]+=a[i-][j];
}
}
int S=<<(n-),ans=1e9;
for(int s=;s<S;s++) {//枚举横切情况
cnt=;
t[cnt++]=;
for(int i=;i<n-;i++) {
if((<<i)&s) t[cnt++]=i+;
}
t[cnt++]=n;
if(cnt->k) continue;
int L=,R=sum,x;
while(L<=R) {
int mid=(L+R)>>;
if(check(mid)) x=mid,R=mid-;
else L=mid+;
}
ans=min(ans,x);
}
printf("%d\n",ans);
return ;
}

TZOJ 4359: Partition the beans (二分)的更多相关文章

  1. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

  2. TZOJ 4471: Postman FJ (二分+bfs)

    描述 FJ now is a postman of a small town in the hills. The town can be represented by a N×N matrix. Ea ...

  3. 二分partition算法应用

    一个二分partition算法,将整个数组分解为小于某个数和大于某个数的两个部分,然后递归进行排序算法. 法一: int partition(vector<int>&arr, in ...

  4. HDU 5646 DZY Loves Partition 数学 二分

    DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...

  5. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  6. zoj 3963 Heap Partition(并查集,贪心,二分)

    Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence S = { ...

  7. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

    E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x =  ...

  8. 二分查找时间复杂度、partition时间复杂度

    二分查找时间复杂度 partition时间复杂度 O(n) = O(n) + O(n/2) + O(n/4)+.... 然后用等比求和公式得出是O(2n),即O(n)

  9. UVALive 2238 Fixed Partition Memory Management(二分完美匹配)

    题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...

随机推荐

  1. idea使用及其快捷键(Jetbrains很多是通用的)(转)

    Java程序员肯定会使用idea进行开发,因为其非常强大,很好用,而且可以很傻瓜式导入gradle,用来做SSM项目也很简单 学生是可以使用教育邮箱或者上床学生证使用免费的jetbrains全家桶的, ...

  2. Python+Django+Ansible Playbook自动化运维项目实战

    Python+Django+AnsiblePlaybook自动化运维项目实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单 ...

  3. Django-rest Framework(三)

    今天看了drf的五个组件的源码,可读性还是很高的,只是读组件的时候要注意的是 大部分的组件都是由dispatch分发出去的,所以看源码的时候一定要抓住dispatch这条主线,一步一步看下去 一. d ...

  4. 字符串匹配算法之kmp算法

    kmp算法是一种效率非常高的字符串匹配算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,所以简称KMP算法 算法思想 在一个字符串中查找另一个字符串时,会遇到如下图的情况 我们通常 ...

  5. 【vue移动端架子】vue-h5-template

    作者大大的地址:https://github.com/sunnie1992/vue-h5-template 我们运行项目,倒是可以看一看效果 虽然就是显示的UI,但是应该可以知道作者大大想要什么东西了 ...

  6. COOK50小结

    题目链接 很遗憾.看到第五题的通过人数就不敢做了.待日后补上. A题 求最长的连续子序列,使得他们满足gcd为1. 如果有相邻的两个数的gcd为1,那么整个序列的gcd值也就是1, 否则就是该序列不存 ...

  7. http方式nginx 访问不带www的域名301重定向跳转到www的域名帮助seo集中权重

    比如我需要吧gucanhui.com重定向301跳转到www.gucanhui.com 需要在nginx的con发文件中加入一段 server { listen ; server_name gucan ...

  8. CSS min-height 属性

    实例 设置段落的最小高度: p { min-height:100px; } 浏览器支持 IE Firefox Chrome Safari Opera 所有主流浏览器都支持 min-height 属性. ...

  9. 洛谷P1979 [NOIP2013提高组Day2T3]华容道

    P1979 华容道 题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少 ...

  10. JavaScript 中的多线程通信的方法

    在Html 5诞生之后,我们可以使用javascript来实现多线程处理.H5 新增了一个web workers api,使用这个API,用户可以很容易地创建在后台运行的线程,H5 中被称为workd ...