zoukankan      html  css  js  c++  java
  • 月度开销

    月度开销

    链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1243
    时间限制: 1000 ms         内存限制: 65536 KB

    【题目描述】

    农夫约翰是一个精明的会计师。他意识到自己可能没有足够的钱来维持农场的运转了。他计算出并记录下了接下来 N (1 ≤ N ≤ 100,000) 天里每天需要的开销。

    约翰打算为连续的M (1 ≤ M ≤ N) 个财政周期创建预算案,他把一个财政周期命名为fajo月。每个fajo月包含一天或连续的多天,每天被恰好包含在一个fajo月里。

    约翰的目标是合理安排每个fajo月包含的天数,使得开销最多的fajo月的开销尽可能少。

    【输入】

    第一行包含两个整数N,M,用单个空格隔开。

    接下来N行,每行包含一个1到10000之间的整数,按顺序给出接下来N天里每天的开销。

     

    【输出】

    一个整数,即最大月度开销的最小值。

    【输入样例】

    7 5
    100
    400
    300
    100
    500
    101
    400

    【输出样例】

    500

    【提示】

    若约翰将前两天作为一个月,第三、四两天作为一个月,最后三天作为一个月,则最大月度开销为500。其他任何分配方案都会比这个值更大。

    题解:二分答案

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int a[100005];
    int main()
    {
        int n,m,l=0,r=1000000008;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            l=max(l,a[i]);
        }    
        if(n==m)
        {
            cout<<l<<endl;
            return 0;
        }
        while(l+1<r)
        {
            int mid=(l+r)/2,t=0,cnt=1;
            for(int i=1;i<=n;i++)
                if(t+a[i]<=mid)t+=a[i];
                else {cnt++;t=a[i];}
            if(cnt>m)l=mid;
            else r=mid;
        }
        int cnt=1,ans,t=0;
        for(int i=1;i<=n;i++)
            if(t+a[i]<=l)t+=a[i];
            else {cnt++;t=a[i];}
        if(cnt<=m)ans=l;
        else ans=r;
        cout<<ans<<endl;
    }
  • 相关阅读:
    POJ 3140 Contestants Division (树dp)
    POJ 3107 Godfather (树重心)
    POJ 1655 Balancing Act (树的重心)
    HDU 3534 Tree (经典树形dp)
    HDU 1561 The more, The Better (树形dp)
    HDU 1011 Starship Troopers (树dp)
    Light oj 1085
    Light oj 1013
    Light oj 1134
    FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8017932.html
Copyright © 2011-2022 走看看