zoukankan      html  css  js  c++  java
  • poj3273——经典的二分优化

    poj3273——经典的二分优化

    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 16522   Accepted: 6570

    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

    题意:将一个数组分成M段,使最大段和最小,求最大段和的最小值
    思路:考虑两种极端情况:分成1组,所有的元素作为一组,则最大段和为总和Sum;分成M组,每一个元素单独作为一组,则最大段和为最大值Max。由于1<=M<=N;故最大段和肯定在[Max,Sum]的范围内,具体等于多少取决于组数M。因此在[Max,Sum]内从大到小进行遍历limit,若找到一个limit,分组数为group,如果group<=M,说明还可以继续分组使limit更小,因此继续查找,如果group>M,说明不能再分了,前一个查找的limit在M分组之下最小值了,直接退出遍历,答案即为limit。此做法时间复杂度为o(N^2),二分优化,在[Max,Sum]进行二分查找,可以使时间复杂度将为o(N*logN)。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    typedef long long ll;
    const int maxn=1000100;
    const ll INF=(1<<28);
    
    int N,M;
    ll a[maxn];
    
    bool judge(int limit)
    {
        int group=1;
        ll sum=0;
        for(int i=0;i<N;i++){
            if(sum+a[i]<=limit) sum+=a[i];
            else{
                sum=a[i];
                group++;
            }
        }
        if(group<=M) return true;///分的组数少了,还可以再分使limit更小
        return false;
    }
    
    ll BinSearch(ll low,ll high)
    {
        while(low<high){
            ll mid=(low+high)/2;
            if(judge(mid)) ///期望mid越小越好,符合条件,继续找更小的
                high=mid;
            else low=mid+1; ///不符合条件,向更大的找
        }
        return high;
    }
    
    int main()
    {
        while(cin>>N>>M){
            ll Max=-INF,Sum=0;
            for(int i=0;i<N;i++){
                cin>>a[i];
                Sum+=a[i];
                if(a[i]>Max) Max=a[i];
            }
            cout<<BinSearch(Max,Sum)<<endl;
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    【kafka学习笔记】PHP接入kafka
    小白要怎么变成大神
    ab测试
    安装matlab出现ERROR8错误
    关于uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
    朴素贝叶斯文本分类
    第10组 Beta冲刺 (2/5)(组长)
    第10组 每周小结 (1/3)(组长)
    第10组 Beta冲刺 (3/5)(组长)
    第10组 Beta冲刺 (5/5)(组长)
  • 原文地址:https://www.cnblogs.com/--560/p/4377422.html
Copyright © 2011-2022 走看看