题目链接:https://vjudge.net/problem/POJ-3273

把n个连续的数字划分成m个连续的部分,每个部分都有一个部分和(这个部分所有值加起来),现在要使划分里最大的那个部分和最小。

我用的也是二分,用二分枚举最大的部分和。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<cstdio>
using namespace std;
#define eps 1e-8
#define ll long long
#define INF 0x3f3f3f3f
ll n,m,money[];
ll jug(ll mid) //循环一次判断mid
{
ll cnt=; //计数
ll sum=money[];
cnt++;
if(sum>mid)
return ;
if(cnt>m)
return ;
for(int i=;i<n;i++)
{
if(sum+money[i]<=mid)
{
sum+=money[i];
}
else
{
sum=money[i];
if(sum>mid)
return ;
cnt++;
if(cnt>m)
return ;
}
}
return ;
}
ll two_(ll l,ll r) //二分求最小
{
ll mid,pos;
while(l<=r)
{
mid=(l+r)/;
if(jug(mid)) //判断这个mid值是否可行,
{
r=mid-;
pos=mid; //pos记录当前值,这种写法比较保险,因为r值不一定可行,但是mid值是一定可行的
}
else
{
l=mid+;
pos=l;
}
}
return pos;
}
int main()
{
while(cin>>n>>m)
{
ll sum=;
for(int i=;i<n;i++)
{
cin>>money[i];
sum+=money[i];
}
cout<<two_(,sum)<<endl;
}
return ;
}

二分 poj 3273的更多相关文章

  1. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  2. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  3. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  4. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  5. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  6. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  7. poj 3273 Monthly Expence 简单二分

    /** 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大 ...

  8. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  9. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

随机推荐

  1. VMware设置cpu虚拟化,intel VT-x

    1.关闭虚拟机 2.右键需要更改的虚拟机--设置--处理器

  2. 4.高级js--(面向对象js)_2

    1.创建对象的三种方式: l第一种构造法:new  Object var a = new Object(); a.x = 1, a.y = 2; l第二种构造法:对象直接量 var b = { x : ...

  3. makefile中 $@, $^, $<, $?含义

    $@ 表示目标文件 $^ 表示所有的依赖文件 $< 表示第一个依赖文件 $? 表示比目标还要新的依赖文件列表 例子 root_num.exe: root_num.o my_root.o gcc ...

  4. 机器学习进阶-疲劳检测(眨眼检测) 1.dist.eculidean(计算两个点的欧式距离) 2.dlib.get_frontal_face_detector(脸部位置检测器) 3.dlib.shape_predictor(脸部特征位置检测器) 4.Orderdict(构造有序的字典)

    1.dist.eculidean(A, B) # 求出A和B点的欧式距离 参数说明:A,B表示位置信息 2.dlib.get_frontal_face_detector()表示脸部位置检测器 3.dl ...

  5. 关于webstorm链接不上SVN的解决办法

    使用WEBSTROM上传代码是很方便的,但是通过它调用SVN时,经常会出现问题,我在使用它调用TortoiseSVN时就出现了一些问题,好在问题已经解决,现在把解决办法分享给大家: 首先,看看,安装时 ...

  6. DD-WRT动态更新WAN口MAC

    将代码在command窗口粘贴后,另存为startup,然后重启路由即可 #!/bin/ash MAC=`(date; cat /proc/interrupts) | md5sum | sed -r ...

  7. 一个关于EasyUI超恶心的BUG。。。Cannot read property 'options' of undefined

    控制台Console抛出的异常: jquery.easyui.min.js:9148 Uncaught TypeError: Cannot read property 'options' of und ...

  8. eclipse git 忽略文件

    ps:git中只有.gitignore文件需要先加索引再提交,其它的都可以直接提交

  9. 吴裕雄 01-mysql管理

    SHOW DATABASES; update user set authentication_string = password("123456") where user='roo ...

  10. cv2.SIFT() AttributeError: 'module' object has no attribute 'SIFT' OpenCV Python can't use SURF, SIFT

    参考链接: https://stackoverflow.com/questions/18561910/opencv-python-cant-use-surf-sift For recent infor ...