Problem description
There is a Yokan of length [cm]. There are cutting points, and the -th cutting point from the left is at position [cm] from the left.
You want to choose of the cutting points and divide the Yokan into pieces. Then, the following value is used as the score:
- The length of the shortest piece among the pieces
Find the score that can be obtained when dividing the Yokan to maximize the score.
Constraints
Input
N L
K
A_1 A_2 A_3 ... A_N
Output
The score need to be find
Examples
Input 1:
7 45
2
7 11 16 20 28 34 38
Output 1:
12
Input 2:
3 100
1
28 54 81
Output 2:
46
Approach
We can employ the binary search algorithm to determine the highest achievable score. During each search iteration, we validate if the target score is feasible with a minimum of K cuts.
Complexity Analysis
- time complexity :
- space complexity :
Solution
#include <iostream>
#include <vector>
using namespace std;
int N, K, L;
vector<int> A;
bool validate(int T) {
int cnt = 0, pre = 0;
for (int i = 0; i < N; i++) {
if (A[i] - pre >= T && L - A[i] >= T) {
cnt++;
pre = A[i];
}
}
return cnt >= K;
}
int main(){
cin >> N >> L;
cin >> K;
A = vector<int>(N, 0);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int l = 0, r = L;
while (l < r) {
int m = l + (r - l + 1) / 2;
if (validate(m)) {
l = m;
} else {
r = m - 1;
}
}
cout << l << endl;
return 0;
}