• 【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;
    }
  • 相关阅读:
    Android拷贝工程不覆盖原工程的配置方法
    Android中BindService方式使用的理解
    getApplicationContext()、Activity.this、 getBaseContext区别
    深入理解ASP.NET MVC(4)
    深入理解ASP.NET MVC(1)
    深入理解ASP.NET MVC(2)
    深入理解ASP.NET MVC(目录)
    C# 代码优化
    NPOI 创建Excel 设置宽度 样式 颜色对比表
    2. Mysql数据库的入门知识
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4888609.html
Copyright © 2020-2023  润新知