Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19207 | Accepted: 7630 |
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
题意是给出一系列数,将这些数按顺序分成M组,求每组和的最小值是多少。
枚举答案,left是一个数一个组,即最大值。right是所有数的和。
每次去和M比较来判断mid是多是少。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; int N,M; int value[100000]; int solve(int mid) { int i,sum=0,result=1; if(mid==501) i=1; for(i=1;i<=N;i++) { if(value[i]+sum<=mid) { sum += value[i]; } else { sum=value[i]; result++; } } if(result>M) { return 1; } else return -1; } int main() { int i,sum,max_x; cin>>N>>M; sum=0; max_x=0; for(i=1;i<=N;i++) { cin>>value[i]; sum += value[i]; max_x=max(max_x,value[i]); } int mid=(sum+max_x)/2; while(max_x<sum) { int temp=solve(mid); if(solve(mid)==-1) { sum = mid-1; } else { max_x = mid+1; } mid = (max_x+sum)/2; } cout<<mid<<endl; return 0; }