Monthly Expense(二分)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11196 | Accepted: 4587 |
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 题意简化后的意思就是给出N个数,将这N个数分成M份,每份必须是连续的一个或几个数,要求分的各份的数之和尽可能小,求出M份和中的最大值;
注意,也许在low == high 之前已分成m组,但可能不是最优的,当while(low < high)结束后得到的low肯定是最优结果;
#include<stdio.h>
#include<string.h>
const int N = ;
int n,m;//把n个数分成m组;
int money[N+];
//计算当前mid值能把n个数分成的组数并与m比较;
bool judge(int mid)
{
int sum = money[];
int group = ; for(int i = ; i < n; i++)
{
if(sum + money[i] <= mid)
{
sum += money[i];
}
else
{
group++;
sum = money[i];
}
} if(group > m)
return false;
else return true;
} int main()
{
int low = ,high = ;//low为下界,high为上界;
int mid;
scanf("%d %d",&n,&m);
for(int i = ; i < n; i++)
{
scanf("%d",&money[i]);
high += money[i];//high初始化为n个数的和,相当于把n个数分成一组;
if(low < money[i])
low = money[i];//low初始化为n个数中的最大值,相当于把n个数分成n组;
} mid = (low+high)/;
while(low < high)
{
if(!judge(mid))//如果mid把n个数分得的组数大于m,说明mid偏小,更新low;
{
low = mid+;
}
else high = mid-;//否则说明mid偏大,更新high;
mid = (low+high)/;
}
printf("%d\n",mid);
return ;
}
Monthly Expense(二分)的更多相关文章
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- POJ3273 Monthly Expense —— 二分
题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Tota ...
- POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7630 ...
- POJ3273:Monthly Expense(二分)
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 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
随机推荐
- JS 拼凑字符串
和Java一样,JS中直接用"+"号拼凑字符串是很耗费资源的,所以在大量拼凑字符串的情景中,我们也需要一个类似于StringBuffer的工具, 下面利用Array.join()方 ...
- CakePHP的blog教程三
简单的身份验证和授权应用 接着我们blog教程的例子,如果我们想要建立一个根据登录的用户身份来决定其安全访问到正确的urls. 同时我们还有其他的需求: 允许我们的blog有多个作者,每一个作者都可以 ...
- 使用Java BigDecimal进行精确运算
首先我们先来看如下代码示例: public class Test_1 { public static void main(String[] args) { System.out ...
- 自定义图文混排视图MyImageTextView
http://blog.csdn.net/xujunfeng000/article/details/36399339?utm_source=tuicool&utm_medium=referra ...
- sql查询过程中 update,insert,delete可视化收影响行数
insert into test_tb output inserted.id,inserted.data values('c'),('d') delete from test_tb output de ...
- C++中extern关键字使用(转)
参考文章:http://blog.csdn.net/sruru/article/details/7951019 chapter1.如何混合编译C语言和C++ 实际开发过程中,C++中会调用C与语言编写 ...
- PHP 用Class构造JSON数据
header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...
- Yii2的相关学习记录,前后台分离及migrate使用(七)
最近一直忙其它的(其实是懒!),将<深入理解Yii2>看了一遍,一些当初没明白的稍微明了了点,然后又看yii2的图片上传等处理.富文本.restful什么的,但由于没进行到这里,只看也不管 ...
- Java高精度学习第二弹——求N!
继续学习Java高精度,今天写的是求N!. 首先附上源代码: import java.util.Scanner; import java.math.BigInteger; public class M ...
- bzoj1038: [ZJOI2008]瞭望塔
Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...