//♥God will make a way♥
//#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <set>
#include <map>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stdio.h>
#include <climits>
#include <numeric>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template <typename T>
//using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/////////////////////define/////////////////////
#define ci(x) if(x) cout << "YES" << '\n'; else cout << "NO" << '\n';
#define cii(x) if(check(x))
#define MOD 1000000007
#define MOD2 998244353
#define oo 1e9
#define ool 1e18L
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mii map<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector <ll>
#define ff first
#define ss second
#define mp make_pair
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define alll(x) (x), (x) + n
#define clr(x) (x).clear();
#define fri(x) for(int i = 0; i < x; ++i)
#define frj(x) for(int j = 0; j < x; ++j)
#define frp(x) for(int p = 0; p < x; ++p)
#define frr(a, b) for(int i = a; i < b; ++i)
#define frrj(a, b) for(int j = a; j < b; ++j)
#define fra(x) for(int i = 0; i < x; ++i) cin >> a[i];
#define frb(x) for(int i = 0; i < x; ++i) cin >> b[i];
#define frs(x) for(auto it = x.begin(); it != x.end(); ++it)
#define fr(x) for(auto it : x) //el
#define fastio ios_base::sync_with_stdio(false); cin.tie(0);
#define dbg(x) cerr << #x << ": " << x << endl;
#define ce(x) cout << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin()); //make all one after sorting
#define blt __builtin_popcount
/////////////////////print array, vector, deque, set, multiset, pair, map /////////////////////
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]"; cout << endl;}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]"; cout << endl;}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]"; cout << endl;}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]"; cout << endl;}
template <class T> void print(stack <T> v) {cerr << "[ "; stack<T> s = v; while(s.size()) {T i = s.top(); print(i); s.pop(); cerr << " ";} cerr << "]"; cout << endl;}
template <class T> void print(queue <T> v) {cerr << "[ "; queue<T> s = v; while(s.size()) {T i = s.front(); print(i); s.pop(); cerr << " ";} cerr << "]"; cout << endl;}
template <class T> void print(deque <T> v) {cerr << "[ "; deque<T> s = v; while(s.size()) {T i = s.front(); print(i); s.pop_front(); cerr << " ";} cerr << "]"; cout << endl;}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]"; cout << endl;}
template <class T, class V> void print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]"; cout << endl;}
/////////////////////de/////////////////////
ll ans, a[5005], dp[5005], best[5005]; //i hat tiv unenq, verjin bloky p...j a(j>=p)
int id[5005];
struct fenwick {
int size;
vector<long long> tree;
void init(int s) {
tree.assign(s + 2, 0);
size = s;
}
void upd(int u, long long v) {
while(u <= size) {
tree[u] += v;
u += u & (-u);
}
}
long long qry(int u) {
long long sum = 0;
while(u > 0) {
sum += tree[u];
u -= u & (-u); // lsb
}
return sum;
}
long long sum(int l, int r) {
return qry(r) - qry(l - 1);
}
} ft1, ft2;
int main() {
fastio;
int n; cin >> n; fra(n)
fri(n) id[a[i]] = i;
frr(1, 1 + n) dp[i] = 1e18;
frr(1, 1 + n) {
// dbg(i)
frrj(1, i+1) { //dp[j-1] ic upd enq anelu, j...i tvery shrjac vercnenq mer taza blokum
// dbg(j)
ll cost = 0;
ft1.init(n+1); ft2.init(n+1);
frrj(1, 1 + n) ft2.upd(j, 1);
vector<pii> v;
for(int p = i; p >= j; p--)
v.pb({id[p], p});
sort(all(v));
int last = 0;
for(auto it : v) {
int p = it.ss;
for(int i = last; i < id[p]; i++){
ft1.upd(a[i], 1);
ft2.upd(a[i], -1);
}
last = id[p];
cost += ft1.sum(i+1, n+1) + ft2.sum(p+1, i+1);
// dbg(p) dbg(cost) dbg(ft1.sum(i+1, n+1)) dbg(ft2.sum(p+1, i+1))
//if(a[t] > i) if(a[t] <= i && a[t] > p)
}
dp[i] = min(dp[i], dp[j-1] + cost);
}
}
cout << dp[n];
return 0;
}
/*
5
3 5 2 4 1
i: 1
j: 1
p: 1
cost: 4
i: 2
j: 1
p: 2
cost: 2
p: 1
cost: 5
j: 2
p: 2
cost: 2
i: 3
j: 1
p: 3
cost: 0
p: 2
cost: 1
p: 1
cost: 3
j: 2
p: 3
cost: 0
p: 2
cost: 1
j: 3
p: 3
cost: 0
i: 4
j: 1
p: 4
cost: 1
p: 3
cost: 2
p: 2
cost: 4
p: 1
cost: 5
j: 2
p: 4
cost: 1
p: 3
cost: 2
p: 2
cost: 4
j: 3
p: 4
cost: 1
p: 3
cost: 2
j: 4
p: 4
cost: 1
i: 5
j: 1
p: 5
cost: 0
p: 4
cost: 0
p: 3
cost: 2
p: 2
cost: 3
p: 1
cost: 3
j: 2
p: 5
cost: 0
p: 4
cost: 0
p: 3
cost: 2
p: 2
cost: 3
j: 3
p: 5
cost: 0
p: 4
cost: 0
p: 3
cost: 2
j: 4
p: 5
cost: 0
p: 4
cost: 0
j: 5
p: 5
cost: 0
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |