POJ-3273 Monthly Expense (最大值最小化问题)

/* Monthly Expense
Time Limit: 2000MS        Memory Limit: 65536K
Total Submissions: 10757        Accepted: 4390
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.
Source

USACO 2007 March Silver
*/
/*坑啊,二分法绝对不能用递归来做,应该用非递归while循环做*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 100100
int a[maxn],N,M,x,y,minx;
void binary(int x1)
{
      if(x>y)
    return ;
    int sum=0;
    int t=0;
    for(int i=0; i<N;)
    {
        if(sum+a[i]>x1)
        {
            t++;
            sum=0;
        }
        else
        {
            sum+=a[i];
            if(i==N-1)
            t++;
            i++;
        }
    }
    int mid;
    if(t<=M)
    {
        minx=x1;
        //minx= min(x1,minx);
        //if(x1>minx)
        //return ;
        y=x1-1;
        mid=(x+y)>>1;
        binary(mid);
    }
    if(t>M)
    {
        x=x1+1;
        mid=(x+y)>>1;
    }
    binary(mid);
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {

        y=0,x=0;
        for(int i=0; i<N; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>x)
                x=a[i];
            y+=a[i];
        }
        minx=1000000000;
        binary((x+y)>>1);
        printf("%d\n",minx);
    }
    return 0;
}
//非递归做的
#include <iostream>
#include <algorithm>
using namespace std;
int money[100005];
int main()
{
    int N;//总共的天数
    int M;//分成fajomonths月数
    cin>>N>>M;
    int low=0;
    int high=0;
    for(int i=1;i<=N;i++)
    {    
        cin>>money[i];
        high+=money[i];
        if(money[i]>low)
            low=money[i];
    }
    int mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        int sum=0;
        int duanshu=1;
        for(int i=1;i<=N;i++)
        {
            sum+=money[i];
            
            if(sum<=mid)
            ;//计算在月最大消费控制在mid下时能分的段数
            else 
            {
                sum=money[i];
                duanshu++;
            }
        }
        if(duanshu>M)//分的段数比需要的大,说明标准高了
            low=mid+1;
        else if(duanshu<M)//分的段数比需要的小,说明标准低了
            high=mid-1;
        else if(duanshu==M)//分的段数和需要的相同,这时还有可能再严格一点,即使每月的最大支出更小一点
            high=mid-1;
    }

    cout<<low<<endl;
    return 0;
}

 

posted @ 2013-07-25 15:29  myth_HG  阅读(314)  评论(0编辑  收藏  举报