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 ...
随机推荐
- swiper 报错 ‘ Can't find variable: Dom7’
一般报这个错是因为浏览器兼容问题,例如低版本的IE 现在通过npm install swiper 安装的版本都是4.x的 我的解决方法就是安装低版本的swiper, npm install swipe ...
- H5C3--媒体查询(向上兼容,向下覆盖),link中引入的ont and only
一.使用 实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- scrollbar 滚动条
滚动条样式:自定义元素的滚动条 <!DOCTYPE html> <html> <head lang="en"> <meta charset ...
- 前端(jQuery)(8)-- jQuery元素遍历
1.向下遍历 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 你不知道的javascript -- 数据类型
1. 数据类型 在js中有7中数据类型 其中6种是基本类型 包括 null, undefined, boolean, number, string和symbol,还有一种是引用类型object 但是判 ...
- Hackerrank--Savita And Friends(最小直径生成树MDST)
题目链接 After completing her final semester, Savita is back home. She is excited to meet all her friend ...
- Idea代理设置与Java程序的代理设置
最近在学习WebService的过程中,为了弄清楚发送和接收的包的数据结构,使用Fiddler抓取包的数据.开始先配置了Idea的代理设置,但执行Java代码发送请求时,依然无法在Fiddler中抓取 ...
- postman发送get和post请求
一.postman发送get请求 在地址栏里输入请求url(用到拼接方式):http://127.0.0.1:8081/getuser?userid=1 选择“GET”方式, 点击“send”得到 ...
- python中os模块简介
一.什么是os模块 os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道,所以离不了 ...
- storm 为什么要存在不透明分区事务
不透明分区事务不区分发新消息还是旧消息,全部用emitPartitionBatch搞定,虽然 emitPartitionBatch返回的X应该是下一批次供自己使用(emitPartitionBatch ...