Question http://acm.hdu.edu.cn/showproblem.php here? PID = 1, 4004
I have never done a second answer before. (Despise me ...)
Obviously, the answer to this question is a monotonic sequence. Binary can make the complexity of logn
Code:
// Hdoj-4004 binary + verification // 1 <= L <= 1000,000,000, 0 <= n <= 500,000, 1 <= m <= n + 1 # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; # define maxn 500002int L, n, m; int A [maxn]; int check (int x) {int T = x; int CNT = 0; For (INT I = 1; I <n + 2; t = x) // point {While (T> = A [I]-A [I-1]) // I represents the next vertex to be skipped. {T-= A [I]-A [I-1]; I ++;} CNT ++; If (CNT> m) return 0;} return 1 ;} int main () {While (CIN> L> N> m) {A [0] = 0; A [n + 1] = L; for (INT I = 1; I <= N; I ++) // n + 2 points {CIN> A [I];} Sort (, A + n + 2); // sort int min = l % m? L/m + 1: L/m; int max = L; while (Min <max) // [min, Max] {int mid = (min + max)/2; if (check (MID) {max = mid;} else {min = Mid + 1 ;}/// when min = max jumps out of cout <min <Endl ;}}