poj 3273 Monthly Expense

来源:互联网 发布:用友软件电话 编辑:程序博客网 时间:2024/06/16 22:21
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 22721 Accepted: 8894

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 5100400300100500101400

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.

就是我以前做过的题”小明的花费预算“一模一样,

让你按它所给的顺序分成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 );    }}

代码菜鸟,如有错误,请多包涵!!!

如果有帮助记得支持我一下,谢谢!!!

0 0
原创粉丝点击