[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

Monthly Expense

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14158   Accepted: 5697

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

解题思路:

题意为给定一个n个数组成的序列,划分为m个连续的区间,每一个区间全部元素相加,得到m个和,m个和里面肯定有一个最大值,我们要求这个最大值尽可能的小。

用二分查找能够非常好的解决问题。这类问题的框架为,找出下界left和上界right, while(left< right), 求出mid,看这个mid值是符合题意,继续二分。最后right即为答案。

本题中的下界为n个数中的最大值,由于这时候,是要划分为n个区间(即一个数一个区间),left是满足题意的n个区间和的最大值,上届为全部区间的和,由于这时候,是要划分为1个区间(全部的数都在一个区间里面),    1<=m<=n, 所以我们所要求的值肯定在 [left, right] 之间。对于每个mid,遍历一遍n个数,看能划分为几个区间,假设划分的区间小于(或等于)给定的m,说明上界取大了, 那么 另 right=mid,否则另 left=mid+1.

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=100010;
int money[maxn];
int n,m;

int main()
{
    scanf("%d%d",&n,&m);
    int left=-1,right=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&money[i]);
        if(left<money[i])
            left=money[i];
        right+=money[i];
    }
    while(left<right)
    {
        int mid=(left+right)/2;
        int cnt=0;
        int cost=0;
        for(int i=1;i<=n;i++)
        {
            if(cost+money[i]>mid)
            {
                cnt++;//划分区间,不包含当前的money[i]
                cost=money[i];
            }
            else
                cost+=money[i];
        }
        cnt++;//最后一个cost值也要占一天
        if(cnt<=m)
            right=mid;
        else
            left=mid+1;
    }
    cout<<right<<endl;
    return 0;
}

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

时间: 2024-10-11 00:59:19

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)的相关文章

POJ 3273 Monthly Expense 二分答案

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13281   Accepted: 5362 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 r

POJ 3273 Monthly Expense (二分枚举)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15822   Accepted: 6321 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 r

poj 3273 Monthly Expense 二分

题目链接:http://poj.org/problem?id=3273 因为规定天数连续 所以从前到后贪心即可 裸二分 二分专题的特点: 1.很容易写 2.很容易写错 所以要注意: 上下界的取值是否太大太小 加起来会不会爆 最后能不能跳出循环 当对中值进行判定以后 应该如何调整lb和ub 注意lb那个地方应该赋值为mid+1 否则会T到死 #include <cstdio> #include <cstdlib> #include <ctime> #include <

二分搜索 POJ 3273 Monthly Expense

题目传送门 1 /* 2 题意:分成m个集合,使最大的集合值(求和)最小 3 二分搜索:二分集合大小,判断能否有m个集合. 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e5 + 10; 12 const int INF = 0x3f3f

第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to

POJ 3273 Monthly Expense(二分)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17465   Accepted: 6961 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 r

POJ 3273 Monthly Expense 【二分答案】

题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如果mid>=m,说明mid偏小,l=mid+1, 如果mid<m,说明mid偏大,r=mid, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath>

poj 3273 Monthly Expense

题目链接:http://poj.org/problem?id=3273 题目大意:给你n个数把他们连续的分成m组,问最小的那一组的最大值. 思路:用最小的那一个元素和总和进行二分...然后判断最小那组为mid时,分组的个数. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace s

poj 3273 Monthly Expense(二分搜索之最大化最小值)

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