#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define siz(x) int(x.size())
#define ll long long
#define ar array
#define vt vector
#define inf INT_MAX
#define lnf (long long) 1e16
const int nxm = int(1e5) + 7;
int n, k, t[nxm];
namespace sub1 {
void solve() {
vt<bool> f(25, false);
for (int i = 0; i < n; ++i) {
--t[i];
f[t[i]] = true;
}
int ans = 21;
for (int mask = 0; mask < (1<<20); ++mask) {
int lbit = 0;
int ton = 0;
bool ok = true;
for (int i = 0; i < 20; ++i) {
if (mask >> i & 1) {
if (!lbit) {
++ton;
}
}
if (!(mask >> i & 1) && f[i]) {
ok = false;
}
lbit = mask >> i & 1;
}
if (ton <= k && ok) {
ans = min(ans, __builtin_popcount(mask));
}
}
cout<< ans << "\n";
}
};
namespace sub2 {
void solve() {
vt<vt<ll>> dp(5003, vt<ll> (5003, -1));
function<ll(int, int)> work = [&](int x, int y) {
ll &r = dp[x][y];
if (r != -1) {
return r;
}
r = lnf;
if (!y) {
return r;
}
if (!x) {
return r = 1;
}
r = min(r, work(x - 1, y - 1) + 1);
r = min(r, work(x - 1, y) + t[x] - t[x - 1]);
return r;
};
cout << work(n - 1, k) << "\n";
}
}
namespace sub3 {
void solve() {
ll ans = t[n - 1] - t[0] + 1;
priority_queue<ll> q;
for (int i = 0; i < n; ++i) {
q.push(t[i + 1] - t[i] - 1);
}
while (k > 1) {
--k;
ans -= q.top();
q.pop();
}
cout << ans << "\n";
}
};
int subtask() {
if (n <= 20) {
return 1;
} else if (n <= 5000) {
return 2;
}
return 3;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> t[i];
}
if (subtask() == 1) {
sub1::solve();
} else if(subtask() == 2) {
sub2::solve();
} else {
sub3::solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
300 KB |
Output is correct |
2 |
Correct |
56 ms |
212 KB |
Output is correct |
3 |
Correct |
55 ms |
212 KB |
Output is correct |
4 |
Correct |
56 ms |
212 KB |
Output is correct |
5 |
Correct |
55 ms |
212 KB |
Output is correct |
6 |
Correct |
61 ms |
308 KB |
Output is correct |
7 |
Correct |
59 ms |
308 KB |
Output is correct |
8 |
Correct |
56 ms |
304 KB |
Output is correct |
9 |
Correct |
55 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
300 KB |
Output is correct |
2 |
Correct |
56 ms |
212 KB |
Output is correct |
3 |
Correct |
55 ms |
212 KB |
Output is correct |
4 |
Correct |
56 ms |
212 KB |
Output is correct |
5 |
Correct |
55 ms |
212 KB |
Output is correct |
6 |
Correct |
61 ms |
308 KB |
Output is correct |
7 |
Correct |
59 ms |
308 KB |
Output is correct |
8 |
Correct |
56 ms |
304 KB |
Output is correct |
9 |
Correct |
55 ms |
212 KB |
Output is correct |
10 |
Correct |
75 ms |
196640 KB |
Output is correct |
11 |
Correct |
88 ms |
196628 KB |
Output is correct |
12 |
Correct |
147 ms |
196688 KB |
Output is correct |
13 |
Correct |
202 ms |
196712 KB |
Output is correct |
14 |
Correct |
224 ms |
196596 KB |
Output is correct |
15 |
Correct |
193 ms |
196816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
300 KB |
Output is correct |
2 |
Correct |
56 ms |
212 KB |
Output is correct |
3 |
Correct |
55 ms |
212 KB |
Output is correct |
4 |
Correct |
56 ms |
212 KB |
Output is correct |
5 |
Correct |
55 ms |
212 KB |
Output is correct |
6 |
Correct |
61 ms |
308 KB |
Output is correct |
7 |
Correct |
59 ms |
308 KB |
Output is correct |
8 |
Correct |
56 ms |
304 KB |
Output is correct |
9 |
Correct |
55 ms |
212 KB |
Output is correct |
10 |
Correct |
75 ms |
196640 KB |
Output is correct |
11 |
Correct |
88 ms |
196628 KB |
Output is correct |
12 |
Correct |
147 ms |
196688 KB |
Output is correct |
13 |
Correct |
202 ms |
196712 KB |
Output is correct |
14 |
Correct |
224 ms |
196596 KB |
Output is correct |
15 |
Correct |
193 ms |
196816 KB |
Output is correct |
16 |
Correct |
14 ms |
2772 KB |
Output is correct |
17 |
Correct |
12 ms |
2772 KB |
Output is correct |
18 |
Correct |
13 ms |
2736 KB |
Output is correct |
19 |
Correct |
14 ms |
2768 KB |
Output is correct |
20 |
Correct |
18 ms |
2764 KB |
Output is correct |
21 |
Correct |
27 ms |
2816 KB |
Output is correct |
22 |
Correct |
21 ms |
2788 KB |
Output is correct |
23 |
Correct |
22 ms |
2816 KB |
Output is correct |
24 |
Correct |
23 ms |
2716 KB |
Output is correct |