poj 3273 Monthly Expense(二分搜索之最大化最小值)
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money ( ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) days. FJ wants to create a budget for a sequential set of exactly M ( ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of or more consecutive days. Every day is contained in exactly one fajomonth. FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line : Two space-separated integers: N and M
Lines ..N+: Line i+ contains the number of dollars Farmer John spends on the ith day
Output
Line : The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
Sample Output
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $ in any month. Any other method of scheduling gives a larger minimum monthly limit.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
#define N 100006
#define inf 1<<30
int n,m;
int a[N];
bool solve(int x){
int sum=;
int group=;
for(int i=;i<n;i++){
if(sum+a[i]<=x){
sum+=a[i];
}
else{
sum=a[i];
group++;
}
}
if(group>=m) return false;//如果当前的mid值能分成大于等于m的组数,说明mid太小,mid变大的时候组数才能变少
return true;
}
int main()
{ while(scanf("%d%d",&n,&m)==){
int high=;
int low=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
high+=a[i];
if(a[i]>low){
low=a[i];//把花费最多的那天当做是最低low(相当于把n天分为n组)
}
} while(low<high){ int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",low);
}
return ;
}
poj 3273 Monthly Expense(二分搜索之最大化最小值)的更多相关文章
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
随机推荐
- # MongoDB学习笔记(持续更新)
启动mongo服务 sodo mongo 显示数据库(显示数据库名称和大小,单位GB) > show dbs admin (empty) local 0.078GB test 0.078GB t ...
- javadoc简介
Javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档.也就是说,只要在编写程序时以一套特定的标签作注释,在程序编写完成后,通过Java ...
- assertion的语法和语义
.1) 语法表示 在语法上,为了支持assertion,Java增加了一个关键字assert.它包括两种表达式,分别如下: assert expression1; assert expression1 ...
- Java 将自己定义的对象作为HashMap的key
须要继承Map的equals函数和hashCode函数 package com.category; import java.util.HashMap; public class GenCategory ...
- C语言 对数组名取地址
作者 : 卿笃军 你有没有想过,对一个一维数组名取地址,然后用这个地址进行加减运算.这会出现什么样的结果呢? 演示样例: int a[5] = {1,2,3,4,5}; int *p = (int * ...
- 关于 视频同步vsync 信号在不同一时候钟域採样问题
今天调试 视频 4k(3840 x 1920)的vsync信号(时钟为 297Mhz) 进入 170Mhz 的时钟域, 发现输出来的信号信号抖动特别厉害.后来才发现这是不同一时候钟域 造成的影响. 快 ...
- Tree( 树) 组件[4]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...
- 小学生之手(01)之 "for循环"
---恢复内容开始--- 咳咳咳!第一次要写这种东西,要是有不足的地方,请见谅!!!并且感觉在这班门弄斧是不是有点托大了.一向擅长低调的我,在’被逼无奈‘之下,要嚣张一下了......(此处省略500 ...
- java中的变量
变量就是命名的内存空间 1.声明和赋值方式:数据类型 变量名 = 值: 数据类型即划分的内存空间,变量名即划分出的内存空间的名 2.变量必须先声明才能使用,不能使用一个没有经过预先声明的变量:没有 ...
- Path对象
Path是连续的Segment的集合,除了 Path 的第一个Segment和最后一个Segment外,其余的Segment的起始点都是前一个Segment的终止点,即Path对象的中的Segment ...