POJ:3273-Monthly Expense
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32067 Accepted: 12081
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 (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called “fajomonths”. Each of these fajomonths contains a set of 1 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 1: Two space-separated integers: N and M
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
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 $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
解题心得:
1. 题意就是给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是maxSum = max(sum[i]),问这个最大值最小是多少?
2. 最大值最小的问题,二分啊,每次二分枚举然后检查逐步缩小范围。
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n,m,num[maxn];
void init() {
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
}
bool checke(ll sum) {
ll temp = 0,cnt = 1;
for(int i=0;i<n;i++) {
if(num[i] > sum)
return false;
if(temp + num[i] > sum) {
temp = num[i];
cnt++;
} else
temp += num[i];
}
if(cnt <= m)
return true;
return false;
}
ll binary_search() {
ll l = 0,r = 1e9+100;
while(r-l > 1) {
ll mid = (l + r) / 2;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
}
int main() {
while(scanf("%d%d",&n,&m) != EOF ){
init();
ll ans = binary_search();
printf("%lld\n",ans);
}
return 0;
}
POJ:3273-Monthly Expense的更多相关文章
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- [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) ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- 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
传送门 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John is an astounding accounting wiza ...
随机推荐
- C++ VS编译问题--LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
用VS编译时,当出现错误LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏: 这个问题的解决方案为: 1. 找到项目\xx属性\配置属性\清单工具\输 ...
- (C# 基础) 类访问修饰符
C# 中有5个权限修饰符,用于控制对对象的访问权限. 1. public: 访问不受限制. namespace, enum成员,interface成员 隐式的具有public 修饰符,不能在显式添 ...
- Unity3d开发集成Google Admob广告增加收入
在Unity游戏中植入广告是Unity 游戏产品增加收入的一种重要方式,常用的广告有谷歌Admob,百度ssp,腾讯广点通,unity公司的unityads等等,而使用的最多的应该属于谷歌Admob, ...
- C语言问题集
征服C指针:P70#include "stdio.h" char *int_to_str(int int_value){ static char buf[20]; sprintf( ...
- Azure CDN:氮气加速已开启,司机们请做好准备
在上一周,我们向各位小伙伴介绍了通过 Azure CDN 高级版服务为 HTTPS 应用加速的做法,漏掉的小伙伴可以点击这里穿越回去补课哦.那我们今天讲点什么呢?当然是 CDN 最重要的价值:改善应用 ...
- 3.tomcat
1.进入网站http://www.apache.org 2.选择 3.关闭防火墙才可以让别人访问自己
- leetcode:查找
1. word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...
- ABI与编译器:ABI是由内核和工具链定义和实现的
http://book.51cto.com/art/201412/460857.htm <Linux系统编程(第2版)>第1章入门和基本概念,这一章着眼于Linux系统编程的基础概念并从程 ...
- 2018.8.18 servlet使用的会话跟踪除session外还有哪些方式
解释HTTP HTTP是一种无连接的协议,如果一个客户端只是单纯地请求一个文件(HTML或GIF),服务器端可以响应给客户端,并不需要知道一连串的请求是否来自于相同的客户端,而且也不需要担心客户端是否 ...
- 前端css优先级以及继承
1.css优先级以及继承 css具有两大特性:继承性和层叠性 继承性 继承:给父级设置一些属性,子级继承了父级的该属性,这就是我们的css中的继承. 有一些属性是可以继承下来 : color . fo ...