zoukankan      html  css  js  c++  java
  • Monthly Expense POJ 3273 二分

    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.
    ////#include <bits/stdc++.h>
    //#include<iostream>
    //#include<algorithm>
    //#include<cstdio>
    //#include<math.h>
    //#include<map>
    //#include<vector>
    //using namespace std;
    //const int inf = 0x3f3f3f3f;
    //const int mod = 1000000007;
    //const int mx = 100010; //check the limits, dummy
    //typedef long long ll;
    //typedef pair<int, int> pa;
    ////const double PI = acos(-1);
    //ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    //#define swa(a,b) a^=b^=a^=b
    //#define re(i,a,b) for(ll i=(a),_=(b);i<_;i++)
    //#define rb(i,a,b) for(ll i=(b),_=(a);i>=_;i--)
    //#define clr(a) memset(a, 0, sizeof(a))
    //#define lowbit(x) ((x)&(x-1))
    //#define mkp make_pair
    ////void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    //ll  m, n, sum = 0, t;
    //double l, r,  ma = 0, mid = 0;
    //int a[mx];
    //ll num = 0, cnt = 0;
    //bool test(double V)
    //{
    //    ll ans = 0, cnt = 0;
    //    for (int i = 0; i < mx; i++) {
    //        ans += a[i];
    //        if (ans > V) {
    //            ans = 0;
    //            cnt++;
    //            i--;
    //        }
    //    }
    //    cnt++;
    //    if (cnt > m) return false;
    //    else return true;
    //}
    //int main()
    //{
    //    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //    cin >> n >> m;
    //    re(i, 0, n) {
    //        scanf("%d",&a[i]);
    //        num += a[i];
    //        ma = max(ma, (double)a[i]);
    //    }
    //    l = ma, r = num;
    //    while (r - l) {
    //        mid = (r + l) / 2;
    //        if (test(mid))r = mid;
    //        else l = mid;
    //    }
    //    printf("%d\n", int(mid + 0.5));
    //    return 0;
    //}
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int n, m;
    int date[100005];
    void solve(int left, int right, int n)
    {
        int l = left, r = right, i;
        int sum, mon, mid;
        while (l <= r)
        {
            mon = 1; sum = 0;
            mid = (l + r) >> 1;
            for (i = 1; i <= n; i++)
            {
                if (sum + date[i] < mid)
                    sum += date[i];
                else
                {
                    sum = date[i];
                    mon++;
                }
            }
            if (mon > m)
                l = mid + 1;
            else r = mid - 1;
        }
        printf("%d\n", mid);
    }
    int main()
    {
        int r = 0, l = 0, i;
        cin >> n >> m;
        for (i = 1; i <= n; i++)
        {
            scanf("%d", &date[i]);
            if (date[i] > l)l = date[i];
            r += date[i];
        }
        solve(l, r, n);
        return 0;
    }
  • 相关阅读:
    在VS2010中如何添加MSCOMM控件,实现串口通讯
    GroupBox与Panel控件
    如何在VS2010中添加ActiveX控件及使用方法
    如何在vs2010中添加Picture控件
    四线开发经验谈
    socket 、 udp 和 tcp
    文本文件与二进制文件
    文件读写 (流)
    [CTSC1999]家园
    洛谷 P1251 餐巾计划问题
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12657118.html
Copyright © 2011-2022 走看看