Submission #986311

#TimeUsernameProblemLanguageResultExecution timeMemory
986311yanndevGroup Photo (JOI21_ho_t3)C++17
100 / 100
336 ms852 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int n; int dp[5044]; void add(vector<int>& bit, int idx, int val) { for (int i = idx + 1; i <= (int)bit.size(); i += (i & -i)) bit[i] += val; } int sum(vector<int>& bit, int idx) { int ans = 0; for (int i = idx + 1; i > 0; i -= (i & -i)) ans += bit[i]; return ans; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<int> ls (n + 1); for (int i = 1; i <= n; i++) cin >> ls[i]; for (int i = 0; i <= n; i++) dp[i] = 1e16; dp[0] = 0; for (int l = 0; l + 1 <= n; l++) { vector<int> bit (n + 2, 0); vector<int> pos (n + 1, 0); /* we used everything from 1 to l */ int cur = l + 1; for (int i = 1; i <= n; i++) if (ls[i] > l) pos[ls[i]] = cur++; int s = 0; int inv = 0; for (int r = l + 1; r <= n; r++) { // from l + 2 to r + 1 s += pos[r]; inv += sum(bit, pos[r]); add(bit, pos[r], 1); //cout << "from " << l << " to " << r << " s " << s << ' ' << inv << '\n'; dp[r] = min(dp[r], dp[l] + inv + s - ((r * (r + 1) / 2) - (l * (l + 1) / 2))); } } 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...