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;
    }
  • 相关阅读:
    设置matplotlib.pyplot设置画图的坐标系
    [leetcode]238. 除自身以外数组的乘积
    彩色图到灰度图究竟是怎么变换的
    1.1 PIL:Python图像处理类库
    基于GoogLeNet的不同花分类微调训练案例
    消息队列 ActiveMQ的简单了解以及点对点与发布订阅的方法实现ActiveMQ
    解决session共享问题
    linux安装Nginx 以及 keepalived 管理Nginx
    nginx解决服务器宕机、解决跨域问题、配置防盗链、防止DDOS流量攻击
    Nginx实现负载均衡
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4888609.html
Copyright © 2011-2022 走看看