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;
    }
  • 相关阅读:
    1、一条sql查询语句的执行过程
    go 内存分配
    GO Json
    gorm CRUD:读写数据
    go 基于切片的队列实现
    go的错误处理
    grpc
    sqlalchemy 判断字段是否存在
    定时函数
    用Python获取Linux资源信息的三种方法
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12657118.html
Copyright © 2011-2022 走看看