zoukankan      html  css  js  c++  java
  • 【POJ 3273】Monthly Expense

    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18802   Accepted: 7515

    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

     
    二分答案+检查。
    检查很简单,从第一天开始凑数就行啦。
    我说得有点抽象,其实很容易理解。
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int MAXN = 100005;
    
    int n, m, money[MAXN];
    
    bool check(int maxm)
    {
        int cnt = 0;
        for (int sum = 0, i = 0; i < n; ++i)
        {
            if (money[i] > maxm) return false;
            if (sum + money[i] > maxm) cnt++, sum = money[i];
            else sum += money[i];
        }
        return cnt + 1 <= m;
    }
    
    void binarysearch(int l, int r)
    {
        if (l == r)
        {
            printf("%d
    ", l);
            return;
        }
        int mid = (l + r) >> 1;
        if (check(mid)) binarysearch(l, mid);
        else binarysearch(mid + 1, r);
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) scanf("%d", &money[i]);
        binarysearch(1, 1000000000);
        return 0;
    }
  • 相关阅读:
    跟着太白老师学python 10day 函数的动态参数 *args, **kwargs, 形参的位置顺序
    IO 流之字符流的缓冲区
    Java IO异常处理方式
    Java IO 流
    Java 其他对象的 API
    Java 集合框架之 JDK 1.5 新特性
    Java 集合框架工具类
    Java 集合框架之 Map
    Java 集合框架查阅技巧
    Java 集合框架之泛型
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4888609.html
Copyright © 2011-2022 走看看