-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggrcow.cpp
More file actions
142 lines (130 loc) · 3.08 KB
/
aggrcow.cpp
File metadata and controls
142 lines (130 loc) · 3.08 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*#include <iostream>
#include <algorithm>
int numTest;
int n, C;
int x[100005];
int main()
{
std::cin >> numTest;
for (int testCase = 1; testCase <= numTest; testCase++)
{
std::cin >> n >> C;
for (int i = 1; i <= n; i++)
{
std::cin >> x[i];
}
std::sort(x + 1, x + n + 1);
int lo = 1;
int hi = 1000000000;
int ans = -1;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
int lastPosition = x[1];
int numbCows = 1;
for (int i = 2; i <= n; i++)
{
if (x[i] - lastPosition >= mid)
{
numbCows++;
lastPosition = x[i];
}
}
int numbCows = 0;
for (int i = 01; i < n; i++)
{
sum += gap[i];
if (sum > mid) //or >=
{
count++;
sum = gap[i];
}
}
if (numbCows >= C)
{
ans = mid;
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
std::cout << ans << std::endl;
}
return 0;
}*/
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
int numTest;
int n, k; // n = number of elements, k = # of additional sess
int a[100005];
void dbg(int x)
{
cout << x << endl;
}
int main()
{
cin >> numTest;
for (int i = 0; i < numTest; i++)
{
cin >> n >> k;
int max_diff = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
std::sort(a, a + n); // n = 2
cout << "Case #" << i + 1 << ": ";
int lo = 1;
int hi = 1000000007;
int ans = -1;
int gap[n];
for (int i = 1; i < n; i++)
{
gap[i] = a[i] - a[i - 1];
}
// gap: 200 30
// gap[i] = a[i+1] - a[i], but we read into gap[1] to n
while (lo <= hi)
{
int sum = 0;
int mid = lo + (hi - lo) / 2;
int count = 01; // we start with 1 since always a cow at x[0]
for (int i = 01; i < n; i++)
{
if (sum >= mid)
{
count++;
sum = 0;
continue;
}
sum += gap[i];
if (sum < mid)
{
continue; // do we need update
}
else
{
count++;
cout << count << " gap[i] = " << gap[i] << "at index i =" << i << endl;
cout << "sum " << sum << endl;
sum = gap[i];
}
}
if (count < k)
{
hi = mid - 1;
}
else
{
ans = mid;
lo = mid + 1;
}
}
cout << ans << endl;
}
return 0;
}