Monthly Expense
Directly on Chinese
Descriptions
You give a sequence length of N, now let you put them cut parts M (so every is continuous), and then each one has a sum [i], where a is the largest maxSum = max (sum [i]), asked that the minimum is the maximum number?
Entry
Plural sets of input and output
each data of the first line is a two integers N, M (1 <= M <= N <= 100000), followed by N rows, each row an integer V I , indicates that this sequence.
Export
Each output line of a data number, is the number for the maximum value of minimum
SAMPLE INPUT
7 5
100
400
300
100
500
101
400
Sample Output
500
Sample interpretation
For the sample, you can put 100,400 into the first group, 300, 100 into a second group, the third group is divided into 500, 101 into the fourth set, fifth divided into 400 groups, and they are the biggest 500.
Topic Link
https://vjudge.net/problem/POJ-3273
The last number is the beginning and n. Only on behalf of a sub-group.
N is the number of the next largest number. Representative n divided groups;
If the result is mid = (previous + next) / 2; it can be divided according to see how much mid groups. Number of groups than m, the representative value is smaller than the actual mid. Becomes the next mid + 1. Otherwise, the last turn into 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 #defineLong Long LL #define INF 0x3f3f3f3f #define MEM (X, Y) Memset (X, Y, the sizeof (X)) #define MAXN. 5 100000 + the using namespace STD; int N, M; int L, R & lt, MID; // right and left int a [MAXN]; // sequence int Solve () { MID = (L + R & lt) / 2 ; // predict maximum int SUM = 0 ; // current and this int CNT = . 1 ; // there cnt parts for ( int I = 0; I <N; I ++ ) { IF (A + SUM [I] <= MID) // current and this can not be greater than the maximum pre-judgment SUM + = A [I]; the else { SUM = A [I]; CNT ++ ; } IF (CNT> M) // resulting fraction is greater than the parts requirements of the subject, i.e., predict and smaller return 0 ; } return . 1 ; // resulting parts or less parts requirements of the subject, i.e., and predict big } int main () { the while (CIN >> N >> M) { L = R & lt = 0 ; //Initialization for ( int I = 0 ; I <N; I ++ ) { CIN >> A [I]; L = max (L, A [I]); R & lt + = A [I]; } the while (L <= R & lt ) { IF (! Solve ()) // predict and small, the whole value interval to be increased, i.e. a left to right movement range L = MID + . 1 ; the else // predict and large, the entire value range for becomes smaller, i.e., moved to the left and right sections R & lt mid- = . 1 ; } COUT << MID << endl; } }