[二分-最小化最大值] - poj3273-Monthly Expense

原创 2018年04月17日 20:26:14
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32395 Accepted: 12180

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.

题意:

给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是maxSum = max(sum[i]),问这个最大值最小是多少?

题解:

二分枚举maxSum,计算对于当前的maxSum是否可以构成M份,如果小于<=M份,则maxSum还可以再小一点.

需要注意的是下界的选取,maxSum的最小值是序列中的最大值,maxSum至少大于max{num[i]}!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll arr[maxn];
int n,m;
bool check(ll mid) {
    int cnt = 1;
    ll sum = 0;
    for(int i=0;i<n;i++) {
        if(sum + arr[i] <= mid) sum += arr[i];
        else sum = arr[i],cnt++;
    }
    return cnt <= m;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ll left = -1,right = 0;
        for(int i=0;i<n;i++) scanf("%lld",&arr[i]),right += arr[i],left = max(left,arr[i]);
        /// 这里的下界要大于最大的,保证每一天可以单独为一个月.下界要小于其和
        while(left <= right) {
            ll mid = (left+right) / 2;
            if(check(mid)) right = mid - 1;
            else left = mid + 1;
        }
        printf("%lld\n",left);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/79980162

POJ3273-Monthly Expense (最小化最大值)

题目链接:click here~~ 【题目大意】  农夫JF在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值 ...
  • u013050857
  • u013050857
  • 2015-06-03 09:35:24
  • 662

POJ3273-Monthly Expense

  • 2011年07月31日 23:05
  • 6KB
  • 下载

POJ3273:Monthly Expense(二分)

Description Farmer John is an astounding accounting wizard and has realized he might run out of m...
  • libin56842
  • libin56842
  • 2013-12-08 20:02:14
  • 3154

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

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158 ...
  • sr19930829
  • sr19930829
  • 2014-07-23 16:05:24
  • 2311

二分答案方法 — 最大值最小化问题

最大值最小化问题(分治解法) 把一个包含n个正整数的序列划分成m个连续的子序列(每个正整数恰好属于一个序列)。设i个序列的各数之和为S(i),你的任务是让所有S(i)的最大值尽量小 #...
  • u010258605
  • u010258605
  • 2015-01-31 10:03:38
  • 4276

POJ: Drying(二分、值最小化)

题目链接:POJ - 3104 题意:有n件湿衣服,分别给出其含水量。若衣服放在空气中,每分钟含水量会减少1;若把衣服放进烘干器里面则含水量每分钟会减少k,当衣服含水量为0时说明衣服已经干了,现...
  • lisong_jerry
  • lisong_jerry
  • 2017-07-26 22:20:10
  • 82

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

题目链接:http://poj.org/problem?id=3273
  • u013903504
  • u013903504
  • 2014-11-09 21:59:16
  • 504

[ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697  ...
  • sr19930829
  • sr19930829
  • 2014-07-23 17:51:10
  • 1374

poj3273-Monthly Expense典型的二分题目

要把n个数分成m份,使得得到的m份中的最大值尽可能的小; 二分:其上界r是n个数的和(相当于把n个数分成1份),下界l是n个数中的最大值,那么取mid=(l+r)/2,进行二分;用tp计数mid遍历...
  • yujuan_Mao
  • yujuan_Mao
  • 2013-02-21 09:34:46
  • 293

TOJ 2789.Monthly Expense(二分经典题目)

题目链接 : http://acm.tju.edu.cn/toj/showp2789.html Farmer John is an astounding accounting wizard and...
  • muyujinxi
  • muyujinxi
  • 2016-07-21 18:08:21
  • 506
收藏助手
不良信息举报
您举报文章:[二分-最小化最大值] - poj3273-Monthly Expense
举报原因:
原因补充:

(最多只允许输入30个字)