poj_3273_Monthly Expense(二分搜索,最小花費)

Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32155 Accepted: 12113

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


題意:

最小化最大值問題,與最大化最小值問題剛好相反。。。。。
https://www.cnblogs.com/Sunnie69/p/5423817.html    點擊打開鏈接,參考來源
共n個月,給出每個月的開銷.將n個月劃分成m個時間段,求m個時間段中開銷最大的時間段的最小開銷值.
分析:(這個問題與那個牛放入牛欄中的問題剛好相反,一個最小最大,一個最大最小,之前的博客https://blog.csdn.net/chrishuimin/article/details/79871676   點擊打開鏈接

1.最大化最小值:
  相當於n個東西分給m個人,使得每個人至少拿x個,那麼每個人拿夠了就走,給後面的人多留一點,只要能分夠>=m個人就是true,
多的全扔給最後一個人就是了.

2.最小化最大值:
  相當於n個東西分給m個人,每個人至多能拿x個,那麼每個人儘可能多拿一點,給後面的人少留一點,只要能使<=m個人分完這n個東
西就是true,之後每個人隨便拿一點給沒有拿到的人就是了.

最小化最大值問題:
1.即首先求出二分的上下限,這個是每次二分必做的準備工作,上限即爲這一堆東西的總量,下限即單個最大的物品的值。
2.有上下限之後即開始二分,最難寫的部分就出來了,即判斷當前分堆是否合理,在判斷分堆是否合理中,主要的限制因素
   爲兩個,一個是單堆的量肯定不能超過當前的Mid值,另一個是分出的堆數一定不能超過題目要求的M值。
3.由判斷條件即可將二分範圍進行縮小,即,一旦當前Mid值滿足分堆要求,意味着我還可以把Mid值縮小,即把二分的
   right=mid,如果當前Mid值觸犯了判斷條件,即說明當前值還太小,即把left=mid。
4.由以上二分return出來的結果即爲所求值

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = 100005;
const int inf = 100005;
int n,m;
int maxx = 0;
int x[Max];
bool C(int k)//區間的最小花費爲k(類似牛的問題)
{
    int sum = 0,num = 1;
    for(int i=0;i<n;i++)    //n個月的開銷
    {
        sum = sum + x[i];    //分區間(如果一個區間的和大於k了,就要從新開始新的區間求和)
        if(sum > k)  //這個區間的和大於k(最小花費了)
        {
            sum = x[i];  //再開始下一個區間,初始化sum的值。
            num ++;       //記錄分成的區間個數
        }
    }
    if(num <= m)      //最後分成的區間個數
        return true;
    return false;
}
void solve()
{
    int left = maxx ,right = inf;
    while(left < right)
    {
        int mid = (left + right)/2;
        if(C(mid))
            right = mid;
        else
            left = mid+1;
    }
    printf("%d\n",left);
}
int main()
{
    scanf("%d%d",&n,&m);   //n個月,m個時間段(類似m頭牛)

    for(int i=0;i<n;i++)
    {
        scanf("%d",&x[i]);     //輸入n個月的花費
        maxx = max(maxx,x[i]);   //找出其中最大的花費
    }

    //sort(x,x+n);   //爲什麼不排序????
    solve();
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章