Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15822 | Accepted: 6321 |
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
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
Source
#include <cstdio> #include <algorithm> using namespace std; int c[100005]; int main() { int n, m, sum = 0, ma = 0, l, r, mid; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d", &c[i]); sum += c[i]; ma = max(ma, c[i]); } r = sum; l = ma; mid = (l + r) / 2; while(l < r) { int tmp = 1, now = 0; for(int i = 0; i < n; i++) { if(c[i] + now <= mid) now += c[i]; else { now = c[i]; tmp++; } } //这里注意不能反!!因为有等于的情况,因为tmp=m时我们要尽量减小划分值 //这是题目的要求,让各组和尽可能小,反了肯定wa if(tmp > m) l = mid + 1; else r = mid - 1; mid = (l + r) / 2; } printf("%d\n", mid); }
POJ 3273 Monthly Expense (二分枚举)
原文地址:http://blog.csdn.net/tc_to_top/article/details/43526451