Submission #1092367

#TimeUsernameProblemLanguageResultExecution timeMemory
1092367ortsacGroup Photo (JOI21_ho_t3)C++17
0 / 100
1 ms440 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 5005; int dp[MAXN]; int32_t main() { int n; cin >> n; vector<int> v(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i]; } dp[0] = 0; for (int k = 1; k <= n; k++) { //cout << "K is " << k << "\n"; dp[k] = INF; vector<int> nv; vector<int> po(k + 1); for (int i = 1; i <= n; i++) { if (v[i] <= k) { po[v[i]] = nv.size(); nv.push_back(v[i]); } } priority_queue<int> neg; int pos = 0; int calc = 0; int t = 0; for (int i = k; i >= 1; i--) { calc -= ((int)neg.size()); calc += pos; calc += abs(po[i] - k + 1); dp[k] = min(dp[k], dp[i - 1] + calc); while ((!neg.empty()) && (neg.top() == t)) { //cout << "removed " << neg.top() << "\n"; neg.pop(); pos++; } if ((po[i] - k + 1) < 0) { neg.push(po[i] - k + 1 + t); //cout << "added from " << i << " it " << po[i] - k + 1 + t << "\n"; } else pos++; t--; } //cout << dp[k] << "\n"; } cout << dp[n] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...