-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path754.cpp
More file actions
115 lines (101 loc) · 1.88 KB
/
754.cpp
File metadata and controls
115 lines (101 loc) · 1.88 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 71882
* Submitted at: 2018-11-19 18:01:20
*
* User ID: 539
* Username: 55211931
* Problem ID: 754
* Problem Name: Hotel Arrangement
*/
#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
using namespace std;
bool checkReach(vector<int> v, int c, int d)
{
int times = 1;
int current = v.at(0);
for (std::vector<int>::iterator it = v.begin() + 1; it != v.end(); ++it)
{
if (current + d <= *it)
{
times++;
current = *it;
}
if (times >= c)
{
return true;
}
}
if (times >= c)
{
return true;
}
else
{
return false;
}
}
int binarySearch(int l, int r,vector<int> v,int c)
{
int mid = l + (r - l) / 2;
if (checkReach(v, c, mid) && !checkReach(v, c, mid + 1))
{
return mid;
}
if (!checkReach(v, c, mid))
{
return binarySearch(l, mid - 1, v, c);
}
if (checkReach(v, c, mid))
{
return binarySearch( mid + 1, r, v, c);
}
}
int main()
{
int times;
cin >> times;
for (int i = 0;i < times;i++)
{
int customers;
int rooms;
cin >> rooms >> customers;
vector<int> dis;
for (int i = 0;i < rooms;i++)
{
int a;
cin >> a;
dis.push_back(a);
}
sort(dis.begin(), dis.end());
int max = dis.at(dis.size()-1) - dis.at(0);
int min = dis.at(1)-dis.at(0);
int first = dis.at(0);
int second;
for (std::vector<int>::iterator it = dis.begin() + 1; it != dis.end(); ++it)
{
second = *it;
if (min > second - first)
{
min = second - first;
}
first = *it;
}
if (customers == rooms)
{
cout << min << endl;
}
else if (min == max)
{
cout << min << endl;
}
else
{
cout << binarySearch(min, max, dis, customers) << endl;
}
}
return 0;
}