#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = tuple<int, int>;
using vii = vector<ii>;
template <typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto &x : vec) in >> x;
return in;
}
template <typename... Ts>
istream &operator>>(istream &in, tuple<Ts...> &tuple) {
apply([&in](auto&... args) {
((in >> args), ...);
}, tuple);
return in;
}
template <typename... Ts>
auto IN() {
tuple<Ts...> result;
apply([](auto&... args) {
((cin >> args), ...);
}, result);
return result;
}
template <typename T>
void OUT(T&& arg) {
cout << arg << '\n';
}
template <typename T, typename... Ts>
void OUT(T&& x, Ts&&... y) {
cout << x << ' ';
OUT(y...);
}
int main() {
auto [n, k] = IN<int, int>();
vi T(n);
cin >> T;
vii groups{{T[0], T[0]+1}};
vii gaps;
for (int i = 1; i < T.size(); ++i) {
int Ti = T[i];
auto &[f, t] = groups.back();
if (t == Ti) ++t;
else {
gaps.emplace_back(t, Ti);
groups.emplace_back(Ti, Ti+1);
}
}
int ans = n;
k -= groups.size();
// sort gaps by length
stable_sort(gaps.begin(), gaps.end(), [] (auto a, auto b) {
return get<1>(a) - get<0>(a) < get<1>(b) - get<0>(b);
});
int i = 0;
for (; k < 0; ++k) {
int gap_size = get<1>(gaps[i]) - get<0>(gaps[i]);
ans += gap_size;
++i;
}
OUT(ans);
return 0;
}
Compilation message
stove.cpp: In function 'int main()':
stove.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 1; i < T.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
328 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
328 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
436 KB |
Output is correct |
16 |
Correct |
40 ms |
4104 KB |
Output is correct |
17 |
Correct |
38 ms |
4196 KB |
Output is correct |
18 |
Correct |
39 ms |
4188 KB |
Output is correct |
19 |
Correct |
45 ms |
4116 KB |
Output is correct |
20 |
Correct |
39 ms |
4116 KB |
Output is correct |
21 |
Correct |
38 ms |
4188 KB |
Output is correct |
22 |
Correct |
37 ms |
4204 KB |
Output is correct |
23 |
Correct |
38 ms |
4136 KB |
Output is correct |
24 |
Correct |
38 ms |
4152 KB |
Output is correct |