POJ 3273 Monthly Expense

http://fengweiding.blog.163.com/
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17708 Accepted: 7072

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

Line 1: Two space-separated integers:  N and  M 
Lines 2.. N+1: Line  i+1 contains the number of dollars Farmer John spends on the  ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

题目大意:将给定的n天每天的消费,将这n天的消费分成m份,每份都是连续的。求分得的m份中(每份中求和)值最大的。
二分答案:

/* ***********************************************
Author :
Created Time :2015/8/5 12:37:26
File Name :3.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[maxn];
int n,m;
bool check(int x){
int sum=0,num=0;
for(int i=1;i<=n;i++){
sum+=a[i];
if(sum>x){
sum=a[i];
num++;//记录比x大的区间的数量
}
}
if(num>=m)return false;
return true;//分大了
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int l,r,ans;
while(cin>>n>>m){
l=0,r=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>l)l=a[i];
r+=a[i];
}
while(l<r){
ans=(l+r)/2;
if(check(ans)){
r=ans;
}
else{
l=ans+1;
}
}
printf("%d\n",l);
}
return 0;
}


你可能感兴趣的:(POJ 3273 Monthly Expense)