-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4116.cpp
More file actions
executable file
·84 lines (78 loc) · 1.61 KB
/
Copy path4116.cpp
File metadata and controls
executable file
·84 lines (78 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* 排序 + 二分
**/
#include <iostream>
#include <stdio.h>
using namespace std;
template <typename T>
void merge(int lo, int mi, int hi, T* a)
{
T* A = a + lo;
int lb = mi - lo;
T* B = new T[lb];
T* BB = B;
for(int i = 0;i < lb;++ i)
B[i] = A[i];
int lc = hi - mi;
T* C = a + mi;
int cnt = 0;
while(1) {
if ((lb == 0) && (lc == 0)) break;
if (lb == 0) {
A[cnt] = C[0];
++ cnt; ++ C; -- lc;
}
else if (lc == 0) {
A[cnt] = B[0];
++ cnt; ++ B; --lb;
}
else {
if(B[0] < C[0]) {
A[cnt] = B[0];
++ cnt; ++ B; -- lb;
}
else {
A[cnt] = C[0];
++ cnt; ++ C; -- lc;
}
}
}
delete []BB;
}
template <typename T>
void mergeSort(int lo, int hi, T* A)
{
if(hi - lo < 2) return;
int mi = (lo + hi) / 2;
mergeSort(lo, mi, A); mergeSort(mi, hi, A);
merge(lo, mi, hi, A);
}
int N, C, pos[100233];
bool check(int dis) {
int cnt = 1, cur = 0;
for(int i = 1; i < N;++ i) {
if(pos[i] - pos[cur] >= dis) {
++ cnt;
cur = i;
if(cnt >= C) return 1;
}
}
return 0;
}
int main() {
scanf("%d%d", &N, &C);
for(int i = 0;i < N;++ i) {
scanf("%d", &pos[i]);
}
mergeSort(0, N, pos);
int lo = 0, hi = 1e9 + 10;
while(lo <= hi) {
int mi = (lo + hi) / 2;
if(check(mi)) {
lo = mi + 1;
} else {
hi = mi - 1;
}
}
cout << lo << endl;
}