# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
632193 | Ooops_sorry | Group Photo (JOI21_ho_t3) | C++14 | 420 ms | 420 KiB |
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;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
const int INF = 1e9;
struct Fenwick {
vector<int> t;
Fenwick(int n) {
t.resize(n);
}
void inc(int i, int d) {
for (; i < t.size(); i = (i | (i + 1))) {
t[i] += d;
}
}
int get(int r) {
int ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
ans += t[r];
}
return ans;
}
};
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), dp(n + 1, INF), pos(n);
dp[0] = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
//dp[i] - answer for the prefix
for (int i = 0; i < n; i++) {
int ind = 0;
Fenwick t(n - i);
for (int j = 0; j < n; j++) {
if (a[j] < i) continue;
pos[a[j] - i] = ind++;
}
int add = 0;
for (int j = 0; j < n - i; j++) {
add += pos[j] - j;
add += t.get(pos[j]);
dp[j + i + 1] = min(dp[j + i + 1], dp[i] + add);
t.inc(pos[j], 1);
}
}
cout << dp[n] << endl;
return 0;
}
Compilation message (stderr)
# | 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... |