[POJ-3273] Monthly Expense (dichotomy)

Keywords: MySQL iOS less

Monthly Expense

Go straight to Chinese

Descriptions

Give you a sequence of length N, and now let you cut them into M parts (so each one is continuous), and then each one has a sum[i]. The largest one is maxSum = max(sum[i]). Ask what is the minimum of this maximum value?

input

Multiple Input and Output Groups
The first row of each set of data is two integers N,M (1<=M<=N<=100000), followed by N rows, with an integer v per row. i Represents this sequence.

output

Each group of data outputs a number of rows, what is the minimum for this maximum value?

sample input

7 5
100
400
300
100
500
101
400

sample output

500

Exemplar interpretation

For example, you can divide 100,400 into the first group, 300,100 into the second group, 500 into the third group, 101 into the fourth group, 400 into the fifth group, and their maximum is 500.

Title Link

https://vjudge.net/problem/POJ-3273

 

Initially, the last term was the sum of n numbers. Representatives are grouped together.

The next one is the largest of the n. Representatives were divided into n groups.

If the result is mid= (last + next) / 2; see how many groups can be grouped according to mid. The number of arrays is larger than m, which means mid is smaller than the actual value. The next one will be mid+1. Otherwise, the last one will become mid-1.

 

AC code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000+5
using namespace std;
int N,M;
int l,r,mid;//Left and right
int a[Maxn];//sequence
int solve()
{
    mid=(l+r)/2;//Maximum Predicted Value
    int sum=0;//The current one and
    int cnt=1;//Yes cnt share
    for(int i=0; i<N; i++)
    {
        if(sum+a[i]<=mid)//The sum of the current one should not be greater than the maximum value of the prediction.
            sum+=a[i];
        else
        {
            sum=a[i];
            cnt++;
        }
        if(cnt>M)//The number of copies obtained is greater than the number required by the title, i.e. the number of predictions and the number of copies reduced.
            return 0;
    }
    return 1;//The number of copies obtained is less than or equal to the number of copies required by the title, i.e. the prediction is greater than the number of copies required by the title.
}
int main()
{
    while(cin>>N>>M)
    {
        l=r=0;//Initialization
        for(int i=0; i<N; i++)
        {
            cin>>a[i];
            l=max(l,a[i]);
            r+=a[i];
        }
        while(l<=r)
        {
            if(!solve())//If the prediction is smaller, the whole range of values will be larger, that is, the left range will move to the right.
               l=mid+1;
            else//When the prediction is larger, the whole range of values will be smaller, that is, the right range will move to the left.
                r=mid-1;
        }
        cout<<mid<<endl;
    }
}

Posted by jrws on Thu, 10 Oct 2019 13:49:00 -0700