#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;
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;
}
set<ll> postaken;
for (ll i = ns; i < N; ++i)
{
currcost+=(pos[i])-distance(postaken.lower_bound(pos[i]),postaken.end());
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
716 KB |
Output is correct |
24 |
Correct |
6 ms |
608 KB |
Output is correct |
25 |
Correct |
8 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
604 KB |
Output is correct |
27 |
Correct |
6 ms |
604 KB |
Output is correct |
28 |
Correct |
6 ms |
680 KB |
Output is correct |
29 |
Correct |
7 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
716 KB |
Output is correct |
24 |
Correct |
6 ms |
608 KB |
Output is correct |
25 |
Correct |
8 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
604 KB |
Output is correct |
27 |
Correct |
6 ms |
604 KB |
Output is correct |
28 |
Correct |
6 ms |
680 KB |
Output is correct |
29 |
Correct |
7 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
604 KB |
Output is correct |
31 |
Correct |
232 ms |
5468 KB |
Output is correct |
32 |
Correct |
240 ms |
5716 KB |
Output is correct |
33 |
Correct |
236 ms |
5668 KB |
Output is correct |
34 |
Correct |
236 ms |
5316 KB |
Output is correct |
35 |
Correct |
230 ms |
5528 KB |
Output is correct |
36 |
Correct |
243 ms |
5468 KB |
Output is correct |
37 |
Correct |
260 ms |
5468 KB |
Output is correct |
38 |
Correct |
243 ms |
5468 KB |
Output is correct |
39 |
Correct |
231 ms |
5464 KB |
Output is correct |
40 |
Correct |
237 ms |
5536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
716 KB |
Output is correct |
24 |
Correct |
6 ms |
608 KB |
Output is correct |
25 |
Correct |
8 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
604 KB |
Output is correct |
27 |
Correct |
6 ms |
604 KB |
Output is correct |
28 |
Correct |
6 ms |
680 KB |
Output is correct |
29 |
Correct |
7 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
604 KB |
Output is correct |
31 |
Correct |
232 ms |
5468 KB |
Output is correct |
32 |
Correct |
240 ms |
5716 KB |
Output is correct |
33 |
Correct |
236 ms |
5668 KB |
Output is correct |
34 |
Correct |
236 ms |
5316 KB |
Output is correct |
35 |
Correct |
230 ms |
5528 KB |
Output is correct |
36 |
Correct |
243 ms |
5468 KB |
Output is correct |
37 |
Correct |
260 ms |
5468 KB |
Output is correct |
38 |
Correct |
243 ms |
5468 KB |
Output is correct |
39 |
Correct |
231 ms |
5464 KB |
Output is correct |
40 |
Correct |
237 ms |
5536 KB |
Output is correct |
41 |
Execution timed out |
5087 ms |
196288 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |