#define NDEBUG
#ifdef NDEBUG
#define dbg(TXTMSG) if constexpr (false) cerr << "lol"
#define dbgv(VARN) ((void)0)
#define dbgfor(COND) if constexpr (false) for (COND)
#else
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_DEBUG_PEDANTIC 1
#pragma GCC optimize("trapv")
#define dbg(TXTMSG) cerr << "\n" << TXTMSG
#define dbgv(VARN) cerr << "\n" << #VARN << " = "<< VARN << ", line: " << __LINE__ << "\n"
#define dbgfor(COND) for (COND)
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
#define e0 first
#define e1 second
constexpr ll INFTY = 1e11;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> H(N);
for (int i = 0; i < N; ++i)
{
cin >> H[i];
H[i]--;
}
// dp[num sorted][desc chain till]
vector<vector<ll>> dp(N,vector<ll>(N,-INFTY));
vector<ll> currelems = H;
vector<ll> pos(N,-1);
for (ll i = 0; i < N; ++i)
{
pos[H[i]]=i;
}
for (ll ns = 0; ns < N; ++ns)
{
ll currcost = INFTY;
for (ll still = 0; still < ns; ++still)
{
currcost = min(currcost,dp[still][ns-1]);
}
if (ns==0)
{
currcost = 0;
}
oset<ll> postaken;
for (ll i = ns; i < N; ++i)
{
currcost+=(pos[i])-ll(postaken.size()-postaken.order_of_key(pos[i]));
postaken.insert(pos[i]);
dbgv(currcost);
dp[ns][i]=currcost;
}
currelems.erase(currelems.begin()+pos[ns]);
pos.assign(N,-1);
for (ll i = 0; i < (ll)currelems.size(); ++i)
{
pos[currelems[i]] = i;
}
}
ll outp = INFTY;
for (ll still = 0; still < N; ++still)
{
outp = min(outp,dp[still][N-1]);
}
dbgfor (int i = 0; i < N; ++i)
{
dbgfor (int j = 0; j < N; ++j)
{
dbg(i << ":" << j << "=" << dp[i][j]);
}
}
cout << outp;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
53 ms |
5468 KB |
Output is correct |
32 |
Correct |
54 ms |
5524 KB |
Output is correct |
33 |
Correct |
53 ms |
5540 KB |
Output is correct |
34 |
Correct |
53 ms |
5468 KB |
Output is correct |
35 |
Correct |
53 ms |
5544 KB |
Output is correct |
36 |
Correct |
53 ms |
5468 KB |
Output is correct |
37 |
Correct |
53 ms |
5536 KB |
Output is correct |
38 |
Correct |
56 ms |
5712 KB |
Output is correct |
39 |
Correct |
55 ms |
5540 KB |
Output is correct |
40 |
Correct |
53 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
53 ms |
5468 KB |
Output is correct |
32 |
Correct |
54 ms |
5524 KB |
Output is correct |
33 |
Correct |
53 ms |
5540 KB |
Output is correct |
34 |
Correct |
53 ms |
5468 KB |
Output is correct |
35 |
Correct |
53 ms |
5544 KB |
Output is correct |
36 |
Correct |
53 ms |
5468 KB |
Output is correct |
37 |
Correct |
53 ms |
5536 KB |
Output is correct |
38 |
Correct |
56 ms |
5712 KB |
Output is correct |
39 |
Correct |
55 ms |
5540 KB |
Output is correct |
40 |
Correct |
53 ms |
5468 KB |
Output is correct |
41 |
Correct |
2879 ms |
196480 KB |
Output is correct |
42 |
Correct |
2820 ms |
196620 KB |
Output is correct |
43 |
Correct |
2752 ms |
196704 KB |
Output is correct |
44 |
Correct |
2837 ms |
196664 KB |
Output is correct |
45 |
Correct |
2801 ms |
196692 KB |
Output is correct |
46 |
Correct |
2862 ms |
196772 KB |
Output is correct |
47 |
Correct |
2848 ms |
196776 KB |
Output is correct |
48 |
Correct |
2794 ms |
196772 KB |
Output is correct |
49 |
Correct |
2849 ms |
196948 KB |
Output is correct |
50 |
Correct |
2865 ms |
196944 KB |
Output is correct |
51 |
Correct |
2844 ms |
196960 KB |
Output is correct |
52 |
Correct |
2859 ms |
196772 KB |
Output is correct |
53 |
Correct |
2814 ms |
196776 KB |
Output is correct |
54 |
Correct |
2847 ms |
196772 KB |
Output is correct |
55 |
Correct |
2874 ms |
196776 KB |
Output is correct |