POJ3273 Monthly Expense 二分

来源:互联网 发布:网络球机接线图 编辑:程序博客网 时间:2024/05/21 18:01
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 22436 Accepted: 8782

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.

Source

USACO 2007 March Silver


// 题意 把 7天分成 5组 要求连续 求出各种情况中分组中的最大值 要求这个最大值是各种情况下最小的

// 第一个二分吧

// ''这个最大值'' 一定在最大元素 和 所有数和之间 然后二分查找


#include<stdio.h>int n,m;int binaryg(int mid,int *a){    int sum=0;    int group=1;    for(int i=1; i<=n; i++)    {        sum+=a[i];        if(sum<=mid)            continue;        else        {            sum=a[i];            group++;        }    }    if(group>m) return 1;//如果按这个数分组 组数大于m说明这个数小了    else   return 0;}int main(){   int high,low,i,mid;    int  a[100010];    scanf("%d%d",&n,&m);    high=0;    low=0;    for(i=1; i<=n; i++)    {        scanf("%d",&a[i]);        high+=a[i];        if(low<a[i])            low=a[i];    }    mid=(high+low)>>1;    while(low<high)    {        if(binaryg(mid,a))            low=mid+1;        else            high=mid-1;        mid=(low+high)>>1;    }    printf("%d\n",mid);    return 0;}


0 0