Submission #751772

#TimeUsernameProblemLanguageResultExecution timeMemory
751772GrindMachineGroup Photo (JOI21_ho_t3)C++17
100 / 100
597 ms196684 KiB
// Om Namah Shivaya #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x, y) ((x + y - 1) / (y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i, n) for(int i = 0; i < n; ++i) #define rep1(i, n) for(int i = 1; i <= n; ++i) #define rev(i, s, e) for(int i = s; i >= e; --i) #define trav(i, a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a, b); } template<typename T> void amax(T &a, T b) { a = max(a, b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; template<typename T> struct fenwick { int siz; vector<T> tree; fenwick(int n) { siz = n; tree = vector<T>(n + 1); } int lsb(int x) { return x & -x; } void build(vector<T> &a, int n) { for (int i = 1; i <= n; ++i) { int par = i + lsb(i); tree[i] += a[i]; if (par <= siz) { tree[par] += tree[i]; } } } void pupd(int i, T v) { while (i <= siz) { tree[i] += v; i += lsb(i); } } T sum(int i) { T res = 0; while (i) { res += tree[i]; i -= lsb(i); } return res; } T query(int l, int r) { if (l > r) return 0; T res = sum(r) - sum(l - 1); return res; } }; void solve(int test_case) { ll n; cin >> n; vector<ll> a(n+5); rep1(i,n) cin >> a[i]; vector<ll> pos(n+5); rep1(i,n) pos[a[i]] = i; ll cost[n+5][n+5]; memset(cost,0,sizeof cost); rep1(l,n){ // cost(l,r): // consider the array after removing guys > r // now we want to take all guys [l,r] to end and arrange them in order r,r-1,r-2,...,l // cost(l,r) = cost to take to end + cost to rearrange within in end // cost to take to end = sum(#of guys smaller than l that appear after pos[i]), for i = [l..r] // cost to rearrange = #of inversions if we consider guys [l..r] // our final order must be r,r-1,...,l (decreasing order) // so definition of inversions here = #of (i,j) s.t i < j and a[i] < a[j] vector<ll> suff(n+5); rep1(i,n){ if(a[i] < l){ suff[i] = 1; } } rev(i,n,1){ suff[i] += suff[i+1]; } fenwick<ll> fenw(n+5); ll take_to_end = 0; ll inv = 0; for(int r = l; r <= n; ++r){ take_to_end += suff[pos[r]+1]; inv += fenw.query(1,pos[r]-1); fenw.pupd(pos[r],1); cost[l][r] = inv + take_to_end; } } vector<ll> dp(n+5,inf2); dp[0] = 0; rep1(i,n){ rev(j,i,1){ amin(dp[i], dp[j-1] + cost[j][i]); } } ll ans = dp[n]; cout << ans << endl; /* ll n; cin >> n; vector<ll> a(n); rep(i,n) a[i] = i + 1; do{ bool good = true; rep(i,n-1){ if(a[i] < a[i+1] + 2){ conts; } good = false; break; } if(good){ debug(a); } } while(next_permutation(all(a))); */ } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } return 0; }
#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...