Monthly Expense POJ 二分
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<string>
using namespace std;
#define MAXN 100005
#define INF 0x3f3f3f3f
/*
将一个序列分为M段,使得这M段中最大的和 最小!
二分搜索
*/
int n, m, a[MAXN];
bool check(int x)
{
int tmp = ,cnt = ;
for (int i = ; i < n; i++)
{
if (tmp + a[i] <= x)
{
tmp += a[i];
}
else
{
tmp = a[i];
cnt++;
}
}
return (cnt <= m);
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
int beg = , end = , mid,ans;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
end += a[i];
beg = max(a[i], beg);
}
while (beg <= end)
{
mid = (beg + end) / ;
//cout << mid << endl;
if (check(mid))
{
ans = mid;
end = mid - ;
}
else
beg = mid + ;
}
printf("%d\n", ans);
}
}
Monthly Expense POJ 二分的更多相关文章
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- 【POJ - 3273】Monthly Expense (二分)
Monthly Expense 直接上中文 Descriptions 给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是ma ...
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...
- Monthly Expense(二分--最小化最大值)
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...
- POJ3273 Monthly Expense (二分最小化花费)
链接:http://poj.org/problem?id=3273 题意:FJ想把n天分成m组,每组是连续的,同一组的花费加起来算,求所分组情况中最高花费的最低值 思路:二分答案.二分整数范围内的花费 ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
随机推荐
- E20170805-hm
mechanize vt. 使机械化; 用机械装置;
- bzoj4563: [Haoi2016]放棋子(错排+高精)
4563: [Haoi2016]放棋子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 387 Solved: 247[Submit][Status] ...
- github fork项目更改后与原作者同步更新
1.进入你的GitHub发起Pull request 2.选择compare across forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 creat ...
- idea 内测设置
找到安装目录/bin/idea64.exe.vmoptions文件 下面是默认配置 -Xms128m -Xmx750m -XX:ReservedCodeCacheSize=240m -XX:+UseC ...
- Linux 下 Solr的搭建与使用(建议jdk1.8以上)
官方表示solr5之后的版本不再提供对第三方容器的支持(不提供war包了). “旧式”solr.xml格式不再支持,核心必须使用core.properties文件定义. 使用第三方容器的需要自己手动修 ...
- 配置Oracle数据库的开机自启动
每当数据库服务器重启后,都要重新启动数据库的监听和实例,特别是在服务器断电重启.例行维护性的场景下.能否像Windows服务器一样,让实例和监听随着服务的启动而启动呢?答案当然是肯定的,我们可以利用O ...
- 安装cloudermanager时如何正确Configuring TLS Security for Cloudera Manager
不多说,直接上干货! 参考官网 https://www.cloudera.com/documentation/enterprise/5-2-x/topics/cm_sg_config_tls_secu ...
- Squirrel的安装(windows上Phoneix可视化工具)
一.下载安装 下载地址:http://www.squirrelsql.org/下载所需版本 或者 从网址http://www.squirrelsql.org/下载相应版本的squirrel ...
- jsp中session执行机制
- Windows10开启热点
1.以网线的连接方式,已经连接. 2.打开CMD 3. 开启热点 3.1设置热点名称和密码 netsh wlan set hostednetwork mode=allow ssid=name key= ...