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;
}

 

转载于:https://www.cnblogs.com/heqinghui/p/3214455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值