描述

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. TZOJ 4292 Count the Trees(树hash)

    描述 A binary tree is a tree data structure in which each node has at most two child nodes, usually di ...

  2. Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和

    给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...

  3. Puppet master-agent模型搭建

    Puppet master-agent模型工作过程: 基于ssl xmlrpc进行通信,端口8140/tcp agent:默认每隔30分钟向master发送node name和facts,并请求cat ...

  4. 【转载】TSN简介及相关资源

    原文:https://blog.csdn.net/u012692537/article/details/86188392 一.简介 1.1 什么是TSN TSN(Time Sensitive Netw ...

  5. Django项目:CRM(客户关系管理系统)--26--18PerfectCRM实现King_admin搜索关键字

    search_fields = ('name','qq',) 登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html search_fields = ( ...

  6. poj1160 动态规划

    #include<stdio.h> #include<string.h> #define INF 999999999 #define Min(x,y) (x<y?x:y) ...

  7. liunx定时删除文件(产生的日志.........)

    linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...

  8. 使用springMvc对象属性自动封装从jsp向controller传值

    controller通过ModelAndView向前台传传递信息 jsp也可以通过model向controller传递信息 这只是其中的一个办法,还有其他几种方式进行前后端的数据交互 如何才能让spr ...

  9. 【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】

    http://www.cppblog.com/MatoNo1/archive/2012/09/23/191708.html —————————————————————————————————————— ...

  10. git仓库远程连接GitHub

    1 先下载git for windows 然后一直点下一步安装. 2 安装之后,选择git gui.生成ssh 链接 git 公钥. 用命令也可以: $ ssh-keygen -t rsa -C &q ...