题目链接: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. Android自定义ProgressBar样式

    我们使用的进度条多种多样,下面有几种自定义的进度条的样式,下面介绍几个. 进度条的有基本的四种样式: 默认风格的进度条: android:progressBarStyle 水平长型进度条: andro ...

  2. docker pull centos慢问题的解决方案

    1.现象 如果直接docker pull centos 两个小时才down下来8M,很慢 2.解决 [root@localhost network-scripts]# cd /etc/docker [ ...

  3. java.lang.IllegalMonitorStateException异常

    转自:https://blog.csdn.net/qianshangding0708/article/details/48290937

  4. ubuntu-14.04中/boot分区不足的解决办法

    环境:ubuntu-kylin 或者 ubuntu-14.04,/boot单独分区工具:ubuntu的liveCD. 问题: 由于当初安装的时候,看网上说/boot很小,100M足以,于是单独分区,分 ...

  5. delphi 属性编辑器

    RegisterPropertyEditor TPictureEditor = class(TClassProperty)   RegisterPropertyEditor(TypeInfo(TPic ...

  6. air 调用jsfl 执行对应函数,并传参

    jsflPath = "WindowSWF/dt_tool_jsfl/" + event.item.fileName+".jsfl"; var element_ ...

  7. AIR程序调用本地默认应用程序打开本地文件

    当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: var file:File = File.desktopDirectory.resolvePath("test.jpg") ...

  8. flash推流工具<转>

    https://github.com/young-cowboy/young-cowboy.github.io https://www.cnblogs.com/xiaoniuzai/p/7129036. ...

  9. MySQL 创建自定义函数

    语法:Create function function_name(参数列表)returns返回值类型 函数体 函数名,应合法的标识符,不应与系统关键字冲突. 一个函数应该属于某个数据库,可以使用db_ ...

  10. [原]vue - webapp 返回无效 解决方案