Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10557 | Accepted: 4311 |
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> #define maxn 100005 using namespace std; int cost[maxn]; int n,m; bool judge(int x)//用来判断按当前二分值作为题目要求的最大值,所分出的堆是否合理。
{ int s=0,t=0; for (int i=0;i<n;i++) { if (cost[i]>x) return false; if (s+cost[i]>x) { if (t>=m-1) return false; t++; s=cost[i]; } else s+=cost[i]; } return true; } int binary(int max,int sum) //二分部分 { int mid,left=max,right=sum; while (left<right) { mid=left+(right-left)/2; if (!judge(mid)) left=mid+1; else right=mid; } return left; } int main() { int max=0;//二分的下界 int sum=0;//二分的上界 scanf("%d%d",&n,&m); for (int i=0;i<n;i++) { scanf("%d",&cost[i]); max=max>cost[i]?max:cost[i]; sum+=cost[i]; } printf("%d ",binary(max,sum)); return 0; }
此外,提醒一下,POJ上的这道题目如果用cin cout会TLE,估计是数据量的问题,建议用C的输入输出