• POJ:3273-Monthly Expense


    Monthly Expense

    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 32067 Accepted: 12081

    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.


    解题心得:
    1. 题意就是给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是maxSum = max(sum[i]),问这个最大值最小是多少?
    2. 最大值最小的问题,二分啊,每次二分枚举然后检查逐步缩小范围。


    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5+100;
    int n,m,num[maxn];
    
    void init() {
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);
    }
    
    bool checke(ll sum) {
        ll temp = 0,cnt = 1;
        for(int i=0;i<n;i++) {
            if(num[i] > sum)
                return false;
            if(temp + num[i] > sum) {
                temp = num[i];
                cnt++;
            } else
                temp += num[i];
        }
        if(cnt <= m)
            return true;
        return false;
    }
    
    ll binary_search() {
        ll l = 0,r = 1e9+100;
        while(r-l > 1) {
            ll mid = (l + r) / 2;
            if(checke(mid))
                r = mid;
            else
                l = mid;
        }
        return r;
    }
    
    int main() {
        while(scanf("%d%d",&n,&m) != EOF ){
            init();
            ll ans = binary_search();
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    手游部分测试点
    Selenium-xapth定位
    添加git 忽略文件
    cxfreeze打包python程序的方法说明(生成安装包,实现桌面快捷方式、删除快捷方式)
    使用minidom来处理XML的示例
    ElementTree之Xml文档处理
    cx_freeze打包EXE文件
    wxpython的简单的应用
    python获取文件路径, 文件名, 后缀名
    flask-sqlalchemy
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107125.html
Copyright © 2020-2023  润新知