• POJ 3273 二分


    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.

    题目大意:

    给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。最大化最小值即可,二分搜索。

    下见代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn=100005;
    int n,m;
    int a[maxn];
    bool C(int x) //c(x)=能不能把n个数分成最多m个每个组的和不超过x的组。(有点绕,但这是重点,仔细理解)
    {
        int sum=0;
        int num=1;
        for(int i=0;i<n;i++)
        {
            sum+=a[i];
            if(sum>x)
            {
                sum=a[i];
                num++;
            }
        }
        return num<=m;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
             int MAX=0;
             int asum=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
                MAX=max(MAX,a[i]);
                asum+=a[i];
            }
            int lb=MAX,ub=asum,ans=0;
            while(lb<=ub)
            {
                int mid=(lb+ub)/2;
                if(C(mid))
                {
                    ans=mid;
                    ub=mid-1;
                }
                else lb=mid+1;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    


     

  • 相关阅读:
    Visual Studio技巧之打造拥有自己标识的代码模板
    arcgis 10.1 错误(TCP_NODELAY NOT enabled)
    中秋节快乐
    调Windows 7的图片浏览器查看图片
    NetCFSvcUtil.exe and Windows 7
    win7下IIS错误:"无法访问请求的页面,因为该页的相关配置数据无效"的解决方法(转)
    卫星地图下载软件WebImageDowns
    出差陕西镇安
    打印完税证明
    c#定义全局条件编译符号
  • 原文地址:https://www.cnblogs.com/Zeroinger/p/5493931.html
Copyright © 2020-2023  润新知