This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void chmax(int &a, int b) {
if (a < b)
a = b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1), b, c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > 0) {
b.push_back(i);
} else if (a[i] == -1) {
c.push_back(i);
}
}
vector<int> l(n + 1), r(n + 1);
int lst = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == -1)
lst = i;
l[i] = lst;
}
lst = n + 1;
for (int i = n; i >= 1; i--) {
if (a[i] == -1)
lst = i;
r[i] = lst;
}
vector dp(2 * n + 1, vector(n + 1, vector<int>(n + 1, -1)));
dp[0][1][0] = 0;
for (int i = 0; i < 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k < int(b.size()); k++) {
if (dp[i][j][k] == -1)
continue;
for (int p = k; p < int(b.size()); p++) {
int x = b[k];
for (int y : {l[x], r[x]}) {
if (y == 0 || y == n + 1)
continue;
if (1 + (i + abs(x - j) + 1) / 2 <= x) {
chmax(dp[min(2 * n, i + abs(x - j) + abs(x - y))][y][p + 1], dp[i][j][k] + 1);
}
}
}
}
}
}
int ans = 0;
for (int i = 0; i <= 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= int(b.size()); k++) {
ans = max(ans, dp[i][j][k]);
}
}
}
cout << b.size() - ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |