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;
    }
  • 相关阅读:
    【大数据云原生系列】大数据系统云原生渐进式演进最佳实践
    Apache Flink on K8s:四种运行模式,我该选择哪种?
    Istio 运维实战系列(2):让人头大的『无头服务』-上
    istio 常见的 10 个异常
    Prometheus Metrics 设计的最佳实践和应用实例,看这篇够了!
    腾讯会议大规模使用Kubernetes的技术实践
    腾讯云推出云原生etcd服务
    Regionals 2014 Asia
    HDU1754 I Hate It splay
    HNOI2002 营业额统计 splay
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4888609.html
Copyright © 2011-2022 走看看