QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163420 | #5507. Investors | ahsoltan | TL | 118ms | 3756kb | C++17 | 2.0kb | 2023-09-04 03:54:01 | 2023-09-04 03:54:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif
const int INF = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while (tt--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int inv = 0;
vector<vector<int>> e(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[j] < a[i]) {
++inv;
e[i + 1].push_back(inv);
e[j].push_back(-inv);
}
}
}
if (k == 0) {
cout << inv << '\n';
continue;
}
vector<vector<int>> c(n, vector<int>(n));
for (int i = 0; i < n; i++) {
set<int> s;
int cnt = 0;
for (int j = i; j < n; j++) {
for (int x : e[j]) {
if (x > 0) {
s.insert(x);
} else if (x < 0 && s.count(-x)) {
s.erase(-x);
cnt++;
}
}
c[i][j] = cnt;
}
}
vector<int> dp1(n), dp2(n);
auto solve = [&] (auto self, int l, int r, int kl, int kr) -> void {
if (l > r) {
return;
}
int mid = (l + r) / 2;
int mnv = INF;
int mnk = 0;
for (int i = kl; i <= min(mid - 1, kr); i++) {
int v = dp1[i] + (mid - 1 >= i + 1 ? c[i + 1][mid - 1] : 0);
if (v < mnv) {
mnv = v;
mnk = i;
}
}
dp2[mid] = mnv;
self(self, l, mid - 1, kl, mnk);
self(self, mid + 1, r, mnk, kr);
};
for (int i = 1; i < n; i++) {
dp1[i] = c[0][i - 1];
}
for (int i = 1; i < k; i++) {
solve(solve, 0, n - 1, 0, n - 1);
swap(dp1, dp2);
}
int ans = INF;
for (int i = 0; i < n; i++) {
ans = min(ans, dp1[i] + (i < n - 1 ? c[i + 1][n - 1] : 0));
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
2 6 1 4 5 6 2 2 1 6 2 4 5 6 2 2 1
output:
2 0
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 118ms
memory: 3756kb
input:
349 6 2 2 1 2 1 2 1 48 12 42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21 48 12 1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...
output:
1 18 15 145 0 2 1 0 13 13 23 0 0 0 1 0 0 0 0 0 0 0 0 161 3 0 0 1 0 0 0 0 0 0 1 0 3 0 0 1 0 0 1 0 0 1 4 0 0 0 0 0 0 0 0 2 0 2 0 0 8 280 0 0 34 4 0 1 0 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 2 0 0 0 0 0 0 0 8 1 8 0 0 0 0 1 11 5 0 0 0 6 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 13 1 0 0 0 ...
result:
ok 349 lines
Test #3:
score: -100
Time Limit Exceeded
input:
6 1000 333 84886 84887 84732 83775 83776 83777 81234 80288 79661 79243 79244 78520 78521 78522 78523 78524 78525 79041 79042 78509 78120 77073 77432 77433 76111 75362 75363 75364 75365 75366 73979 73980 73981 73982 73983 73984 73472 73473 73075 72948 72949 72727 72728 72383 72384 72272 72273 72274 7...