poj 3273 Monthly Expense
来源:互联网 发布:用友软件电话 编辑:程序博客网 时间:2024/06/16 22:21
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 5100400300100500101400
Sample Output
500
Hint
就是我以前做过的题”小明的花费预算“一模一样,
让你按它所给的顺序分成m份,m份中最大的一份中的最小值
举个例子
5 3
4 5 8 5 5
那就会分成
4 5一份, 8一份, 5 5一份,最大的为10
例题中的
100 400 ,300 100 , 500 , 101,400共五份
最大的为500,
明白吗?
用二分法就可以了,代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int MAX = 100005;int n, m;int a[MAX];int Sep(){ int i; int N; int low = 0, high = 0; for ( i = 0;i < n; i++ ) { if ( a[i] > low ) { low = a[i]; } high = high + a[i]; } while ( low <= high ) { int mid = (low+high)/2; int num = 1; int sum = 0; for ( i = 0;i < n; i++ ) { if ( sum + a[i] <= mid ) { sum = a[i]+sum; } else { sum = a[i]; num++; } } if ( num > m ) { low = mid+1; } else { high = mid-1; N = mid; } } return N;}int main(){ int i; while ( ~scanf ( "%d %d", &n, &m ) ) { for ( i = 0;i < n; i++ ) { scanf( "%d", &a[i] ); } int sum = Sep(); printf ( "%d\n", sum ); }}
代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得支持我一下,谢谢!!!
- poj 3273 Monthly Expense
- poj-3273 Monthly Expense
- poj 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ-3273-Monthly Expense
- POJ 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ - 3273 Monthly Expense
- POJ-3273-Monthly Expense
- POJ 3273 Monthly Expense
- 直方图均衡化
- Ubuntu快速安装jdk 1.8
- js代码执行C#代码
- jvm面试题(三)
- JS组件系列——封装自己的JS组件,你也可以
- poj 3273 Monthly Expense
- Android中直播视频技术探究之---基础知识大纲介绍
- 51Nod-1107-斜率小于0的连线数量
- jni的使用
- log4j和logback
- 二叉排序树
- uva 12657 Boxes in a Line
- 【软工视频】软件实现
- 搞懂指针