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 ...
随机推荐
- 通过SSHFS在RHEL中安全的挂载远程Linux/UNIX目录或文件系统--转载
You can easily mount remote server file system or your own home directory using special sshfs and fu ...
- iOS UIKit:viewController之层次结构(1)
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁.且应用程序至少有一个view controller.每个view controller对象都负责和管理一个 ...
- 第七章----pwm蜂鸣器
Linux代码的重用: 在头文件中加载即可使用到原来所有的功能,还有动态方式就是一个驱动使用另一个驱动的资源. 对于像蜂鸣器这样的实验,它的内部文件有很多,所有要有很多的源代码以用于妥善管理. 有多个 ...
- HTML5 <Audio/>标签Api整理(二)
1.实例2: 相对较完整 Html代码: <style> #volumeSlider .slider-selection { background:#bababa; } </styl ...
- LaTeX 中插入数学公式
一.常用的数学符号 1.小写希腊字母 \alpha \nu \beta \xi \gamma o \delta \pi \epsilon \rho \zeta \sigma \eta \tau \th ...
- 【转】 iOS-Core-Animation-Advanced-Techniques(七)
高效绘图.图像IO以及图层性能 高效绘图 原文:http://www.cocoachina.com/ios/20150106/10840.html 不必要的效率考虑往往是性能问题的万恶之源. ——Wi ...
- javascript基础学习(六)
javascript之对象 学习要点: 对象的属性和方法 对象的原型 一.对象 对象其实就是一种引用类型,而对象的值就是引用对象的实例. 二.创建对象 在javascript中有两种对象,一种是系统内 ...
- mysql数据库容量查询
1.统计每张表的数据量SELECT *FROM ( select TABLE_NAME, concat( round( sum(DATA_LENGTH / 1024 / 1024 ), 7 ) ) a ...
- hdoj 1892(二维树状数组)
Problem H Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- hdoj 2191(多重背包)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/ ...