Submission #1081576

#TimeUsernameProblemLanguageResultExecution timeMemory
1081576IcelastGroup Photo (JOI21_ho_t3)C++17
100 / 100
403 ms604 KiB
#include <iostream> #include <bits/stdc++.h> #define ll long long using namespace std; const ll maxn = 5005, INF = 1e9+9; struct BIT{ int n; vector<int> bit; BIT(int n) : n(n){ bit.resize(n+1, 0); }; void upd(int i, int x){ while(i <= n){ bit[i] += x; i += i&-i; } } int sum(int l, int r){ int res = 0; l--; while(l > 0){ res -= bit[l]; l-=l&-l; } while(r > 0){ res += bit[r]; r-=r&-r; } return res; } }; void solve(){ int n; cin >> n; vector<int> a(n+1), p(n+1); for(int i = 1; i <= n; i++){ cin >> a[i]; p[a[i]] = i; } vector<int> dp(n+1, INF); dp[0] = 0; BIT bit(n+1), bit2(n+1); int d; for(int i = 1; i <= n; i++){ d = 0; bit.upd(p[i], 1); for(int j = 1; j <= i; j++){ bit.upd(p[i-j+1], -1); d += p[i-j+1]-1-bit.sum(1, p[i-j+1])-bit2.sum(1, p[i-j+1]); d += bit2.sum(p[i-j+1], n); dp[i] = min(dp[i], dp[i-j]+d); bit2.upd(p[i-j+1], 1); } swap(bit, bit2); } cout << dp[n]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
#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...