This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 5007, MOD = 1e9 + 7;
int a[MAX], ans[MAX], dp[MAX][MAX], id[MAX];
int bit[MAX][5];
void upd(int x, int v, int ty){
while(x < MAX){
bit[x][ty] += v;
x += x & -x;
}
}
int qry(int x, int ty){
int res = 0;
while(x){
res += bit[x][ty];
x -= x & -x;
}
return res;
}
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
ans[i] = 2e9;
id[a[i]] = i;
}
for(int i = 1;i <= n;i++){
int now = 0;
for(int j = n;j >= 1;j--){
if(i <= a[j]){
int pos = i + n - a[j];
now += qry(pos, 0);
upd(pos, 1, 0);
upd(j, 1, 1);
}
}
for(int j = n;j >= i;j--){
dp[i][j] = now;
upd(id[j], -1, 1);
now += j - i - qry(id[j], 1);
now -= qry(id[j], 1);
now -= qry(id[j], 2);
upd(id[j], 1, 2);
}
for(int j = n;j >= 1;j--){
if(i <= a[j]){
int pos = i + n - a[j];
upd(pos, -1, 0);
}
}
for(int j = n;j >= i;j--)
upd(id[j], -1, 2);
}
for(int i = 0;i < n;i++){
for(int j = i + 1;j <= n;j++){
ans[j] = min(ans[j], ans[i] + dp[i + 1][j]);
}
}
cout << ans[n] << '\n';
}
int main(){
IOS
// CASE
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |