TZOJ 4359: Partition the beans (二分)
描述
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 (二分)的更多相关文章
- 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 ...
- 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 ...
- 二分partition算法应用
一个二分partition算法,将整个数组分解为小于某个数和大于某个数的两个部分,然后递归进行排序算法. 法一: int partition(vector<int>&arr, in ...
- HDU 5646 DZY Loves Partition 数学 二分
DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- zoj 3963 Heap Partition(并查集,贪心,二分)
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 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 = ...
- 二分查找时间复杂度、partition时间复杂度
二分查找时间复杂度 partition时间复杂度 O(n) = O(n) + O(n/2) + O(n/4)+.... 然后用等比求和公式得出是O(2n),即O(n)
- UVALive 2238 Fixed Partition Memory Management(二分完美匹配)
题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...
随机推荐
- Jmeter设置中文汉化
下载汉化包logkit-2.0.jar 将汉化包copy至 jmeter文件的lib目录下 打开Jmeter软件,设置汉化包 Options -> ChooseLanguage -> ...
- MySQL系列(八)--数据库分库分表
在互联网公司或者一些并发量比较大的项目,虽然有各种项目架构设计.NoSQL.MQ.ES等解决比较高的并发访问,但是对于数据库来说,压力 还是太大,这时候即使数据库架构.表结构.索引等都设计的很好了,但 ...
- 关于dex 64K 引用限制
1.官方文档 https://developer.android.com/studio/build/multidex 主要内容: 什么是64K限制 编码时如何避免64K 限制 拆分dex避免64K 限 ...
- Ubuntu中安装gdal python版本
安装过程: python包是从C++包中编译出来的,所以需要将源码下载进行编译安装 1.GDAL中的矢量数据处理OGR依赖于Geos,在安装GDAL之前要安装Geos Geos的下载地址:http:/ ...
- TZ_07_SSM整合
1.坐标版本控制: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- JavaScript实现继承的方式和各自的优缺点
ECMAscript只支持实现继承,主要是依靠原型链来实现的. JavaScript实现继承的方式: 类式继承 构造函数继承 组合继承 寄生组合式继承 1.类式继承 //类式继承 //声明父类 fun ...
- angular依赖注入(1)——从父元素到子元素的数据注入
1.什么是依赖注入? 依赖注入是一种编程模式,可以让类从外部源中获得它的依赖,不必亲自创建他们. (这就达到了一个效果,我不知道我是怎么实现的,但我创建了一个实现他的接口,然后接口封装起来,1.可以分 ...
- mysql8下载安装及配置
mysql8下载和安装 一.下载 官网地址:https://dev.mysql.com/downloads/mysql/8.0.html 选择“downloads”-->"mysql ...
- linux压缩打包
linux下的压缩命令有tar.gzip.gunzip.bzip2.bunzip2. compress.uncompress.zip.unzip.rar.unrar等等,压缩后的扩展名有.tar..g ...
- Laravel 使用 JWT 做 API 认证之tymon/jwt-auth 1.0.0-beta.1实践 - moell - SegmentFault
安装 将"tymon/jwt-auth": "1.0.0-beta.1" 添加到 composer.json 中,执行 composer update Prov ...