Title Description
For a given length is a positive integer number N of columns Ai , want now divided into M ( M ≤ N segment), and each successive requirements, and the maximum and minimum of each segment.
About maximum minimum:
For example a number of columns 42451 to be divided into three stages
Segment which is as follows:
[4 2][4 5][1]
And for the first segment 6, the first two segments and is 9 , the 3 segments and is 1 , and a maximum of 9 .
Segment which is as follows:
[4][2 4][5 1]
And for the first section 4 , the second section is 2 and 6 , the 3 segments and of 6 and a maximum of 6.
And in any case the segment, the maximum value of not less than 6
To get the number of columns can be 42,451 to be divided into three segments, each of the maximum and minimum of 6.
Input Format
The first row 1 comprises two positive integers N, M.
The first row contains 2 N spaces separated by non-negative integer A_i , as meaning the subject.
Output Format
A positive integer, that is how much each segment and maximum minimum.
Sample input and output
5 3 4 2 4 5 1
6
Description / Tips
For 2 0 % of the data, there are n ≤ 10 ;
For . 4 0 % of the data, there N≤1000 ;
For . 1 0 0 % of the data, there N≤100000, M ≦ N, A_i N ≤ . 1 0 0 0 0 0 and no more than 10 ^ 9 .
Ideas:
This question is visible, so that the maximum value of the minimum requirements of each segment, the answer is clear dichotomy, a dichotomy we val, the left point to the maximum value of the sequence, and the right end point for the sequence,
Every two minutes, if only two points satisfies into a limited number of times, r = min-1, or r = mid + 1;
Scanf without spaces note in parentheses, otherwise no output, and,
Read faster in use is to use a semicolon between the number and the number of read, you can not use commas, because C ++ to perform content after the comma, easy pan.
Code:
#include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; int l,r,n,m,a[100100],yilin,sum,mid; bool judge(int x) { yilin=0,sum=0; for (int i=1;i<=n;i++) { if(sum+a[i]<=x) sum+=a[i]; else sum=a[i] , yilin++; } return (yilin >= m); } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); r+=a[i]; l=max(l , a[i] ); } while(l<=r) { mid=(l+r) / 2; if(judge(mid))l = mid + 1; else r = mid-1; } printf("%d\n",l); return 0; }