Submission #936848

#TimeUsernameProblemLanguageResultExecution timeMemory
936848jcelinGroup Photo (JOI21_ho_t3)C++14
100 / 100
526 ms390708 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5007; const ll INF = 1e18 + 7; const int logo = 20; const int off = 1 << logo; ll inv[MAXN][MAXN], dp[MAXN], c[MAXN][MAXN]; //lesrig[y][x] - koliko brojeva je manje od y nadesno a oni su >= x int lesrig[MAXN][MAXN], p[MAXN], a[MAXN], arr[MAXN]; ll vl(ll x){ return (ll)x * (x - 1) / (ll)2; } void solve(){ int n; cin >> n; for(int i=1; i<=n; i++) cin >> arr[i]; for(int L=1; L<=n; L++){ int cp = 1; for(int i=1; i<=n; i++) if(arr[i] >= L){ a[cp] = arr[i]; p[arr[i]] = cp++; } ll su1 = 0, su0 = 0; int id = 1; for(int R=L; R<=n; R++, id++){ int cur = a[id]; if(cur < R) su1 -= p[cur]; else if(cur > R) su0 += p[cur]; if(cur != R){ if(p[R] <= id) su0 -= p[R]; else su1 += p[R]; } c[L][R] = su1 - su0; } } for(int i=0; i<=n; i++) p[i] = 0; for(int pos=n; pos; pos--){ int cur = arr[pos]; for(int x=1; x<=n; x++) lesrig[cur][x] = p[cur - 1] - p[x - 1]; for(int i=cur; i<=n; i++) p[i]++; } for(int R=1; R<=n; R++){ for(int L=1; L<R; L++){ inv[L][R] = inv[L][R - 1] + lesrig[R][L]; } } dp[0] = 0; for(int i=1; i<=n; i++) dp[i] = INF; for(int R=1; R<=n; R++){ for(int L=0; L<R; L++){ int len = R - L; dp[R] = min(dp[R], dp[L] + vl(len) - inv[L + 1][R] + c[L + 1][R]); } } cout << dp[n] << "\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt = 1; //cin >> tt; while(tt--) solve(); 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...