题目链接: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. CustomJSProperties珍藏版。目的是减少客户端的代码数量,但是又能将服务器数据传输给客户端。关键是:数据是实时更新的!!!!

    [这是帮助文档的说明] CustomJSProperties EventThe CustomJSProperties event fires each time a control callback ...

  2. day13-文件操作

    1.打开与关闭 1.1.open() close()我们使用 open() 函数打开文件.这个函数将返回一个文件对象,我们对文件的读写都将使用这个对象.open() 函数需要三个参数,第一个参数是文件 ...

  3. air报错 Error: Error #3000: Illegal path name

    配置增加: <supportedProfiles>extendedDesktop desktop</supportedProfiles> fb: flash:

  4. SourceTree commit information window消失解决办法

    https://answers.atlassian.com/questions/15282793/sourcetree-how-to-show-commit-information-panel 执行命 ...

  5. ReentrantLock 学习笔记

    有篇写的很不错的博客:https://blog.csdn.net/aesop_wubo/article/details/7555956    基于JDK1.8 参考着看源码 ,弄清楚lock()和un ...

  6. 修改window本地hosts文件,修改域名指向

    Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...

  7. [ SHELL编程 ] shell编程中数值计算方法实例

    SHELL编程中经常会涉及到数值计算,有时候对于这些计算命令使用场景容易忘记或者混淆,这里针对常用的命令做个总结.主要包括let.bc.expr.(())等. 1.let 使用格式:let 表达式,表 ...

  8. LeetCode OJ 19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. 去除文件BOM头工具

    <?php /** * 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可.代码来源于网络. * chenwei 注. */ header('content-Type: ...

  10. 关于WSSE验证-- 一种验证用户的方法

    大家通常验证用户做法: 1. BASIC验证模式: 把用户名和密码采用Base64编码之后,放在HTTP HEADER里,发到服务器的. 2. FORM验证模式: 就什么都不处理,直接发到服务器. 3 ...