poj3273--Monthly Expense(二分)

Monthly Expense

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status

Appoint description: 
System Crawler  (2015-01-23)

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组,要求分的组中的最大值最小。

最大值最小的问题,可以采用二分来解决,二分其中一个值,算另一个结果,找到上届或下届。

在这个题中 二分组的最大花费,计算是不是可以分为m组,找出最小的花费。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
LL c[110000] ;
LL n , m ;
int solve(LL k)
{
    int num = 0 , i , sum = 0 ;
    for(i = 0 ; i < n ; i++)
    {
        if( c[i] > k ) return 0 ;
        if( sum+c[i] > k )
        {
            num++ ;
            sum = c[i] ;
        }
        else
            sum += c[i] ;
    }
    if( sum <= k ) num++ ;
    if( num <= m )
        return 1 ;
    return 0;
}
int main()
{
    LL i , j , low , mid , high , last ;
    while( scanf("%I64d %I64d", &n, &m) != EOF )
    {
        low = high = 0 ;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%I64d", &c[i]) ;
            low = max(low,c[i]) ;
            high += c[i] ;
        }
        while( low <= high )
        {
            mid = ( low + high ) / 2 ;
            if( solve(mid) )
            {
                last = mid ;
                high = mid -1 ;
            }
            else
                low = mid + 1 ;
        }
        printf("%I64d\n", last) ;
    }
    return 0;
}

时间: 2024-10-26 11:40:03

poj3273--Monthly Expense(二分)的相关文章

POJ3273 Monthly Expense —— 二分

题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29231   Accepted: 11104 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the

poj3273 Monthly Expense 二分查找

题目链接 题意:给出n组数据,每组有一个数,有多种方法通过将连续的几组数据并为一组使得组数从n减少至k,每组数据的值为其组中数据之和,每种方法得到的k组数据都有最大的一组数据和m,求这些方法中最小的m 解法:二分查找 以n组中最小的数为左边界l,n组数据之和为右边界r进行二分查找,取中间值mid=(l+r)/2,mid即为k组数据中最大的一组数据和,若mid可将n组数据分成多于k组数据,则mid偏小取右间, 若可将n组数据分成小于k组数据,则mid偏小取左区间,若恰好可分成k组数据,因要取最小的

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 <

poj3273 - Monthly Expense

这是一个二分穷举问题,通过二分寻找最小值,不断枚举,但一定要二分结束,否则只要找到m组就结束,不一定是最优解. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=100000+10; int a[maxn]; int m,n; bool judge(int mid) { int g=

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

BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

直接二分答案然后判断. ----------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ;  i < n ; ++i ) #define clr

[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 r

poj3273——经典的二分优化

poj3273——经典的二分优化 Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16522   Accepted: 6570 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already