#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Fenwick
{
int N;
vector<int> BIT;
Fenwick(int n) : N(n+1), BIT(N) {}
void update(int iPos, int delta)
{
for (iPos++; iPos < N; iPos += iPos & -iPos)
BIT[iPos] += delta;
}
int sum(int r) // sum < r
{
int sol(0);
for (; r; r -= r & -r)
sol += BIT[r];
return sol;
}
int sum(int l, int r) // l <= i < r
{
return sum(r) - sum(l);
}
};
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbElem;
cin >> nbElem;
vector<int> hauteurs(nbElem);
vector<int> positions(nbElem);
for (int iPos = 0; iPos < nbElem; ++iPos)
{
cin >> hauteurs[iPos];
--hauteurs[iPos];
positions[hauteurs[iPos]] = iPos;
}
Fenwick fen(nbElem+1);
Fenwick fen2(nbElem+1);
for (int iPos = 0; iPos < nbElem; ++iPos)
fen.update(iPos, 1);
vector<int> dp(nbElem+1, 1e18);
dp[0] = 0;
for (int dejaPlace(0); dejaPlace < nbElem; ++dejaPlace)
{
int coutPlacement = dp[dejaPlace];
for (int placeApres(1); placeApres + dejaPlace <= nbElem; ++placeApres)
{
int iPos = placeApres + dejaPlace - 1;
coutPlacement += fen.sum(positions[iPos]);
coutPlacement -= fen2.sum(positions[iPos]+1, nbElem);
fen2.update(positions[iPos], 1);
/*for (int iPos = dejaPlace + placeApres-1; iPos >= dejaPlace; --iPos)
{
coutPlacement += fen.sum(positions[iPos]);
fen.update(positions[iPos], -1);
}
for (int j(0); j < positions[iPos]; ++j)
if (hauteurs[j] >= dejaPlace and (hauteurs[j] >= dejaPlace + placeApres or hauteurs[j] < iPos))
++coutPlacement;*/
dp[dejaPlace+placeApres] = min(dp[dejaPlace+placeApres], coutPlacement);
}
for (int placeApres(1); placeApres + dejaPlace <= nbElem; ++placeApres)
fen2.update(positions[placeApres+dejaPlace-1], -1);
fen.update(positions[dejaPlace], -1);
}
cout << dp[nbElem] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
508 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
508 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
14 ms |
364 KB |
Output is correct |
32 |
Correct |
15 ms |
364 KB |
Output is correct |
33 |
Correct |
15 ms |
512 KB |
Output is correct |
34 |
Correct |
14 ms |
492 KB |
Output is correct |
35 |
Correct |
15 ms |
364 KB |
Output is correct |
36 |
Correct |
13 ms |
364 KB |
Output is correct |
37 |
Correct |
13 ms |
364 KB |
Output is correct |
38 |
Correct |
15 ms |
512 KB |
Output is correct |
39 |
Correct |
16 ms |
364 KB |
Output is correct |
40 |
Correct |
13 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
508 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
14 ms |
364 KB |
Output is correct |
32 |
Correct |
15 ms |
364 KB |
Output is correct |
33 |
Correct |
15 ms |
512 KB |
Output is correct |
34 |
Correct |
14 ms |
492 KB |
Output is correct |
35 |
Correct |
15 ms |
364 KB |
Output is correct |
36 |
Correct |
13 ms |
364 KB |
Output is correct |
37 |
Correct |
13 ms |
364 KB |
Output is correct |
38 |
Correct |
15 ms |
512 KB |
Output is correct |
39 |
Correct |
16 ms |
364 KB |
Output is correct |
40 |
Correct |
13 ms |
364 KB |
Output is correct |
41 |
Correct |
774 ms |
620 KB |
Output is correct |
42 |
Correct |
793 ms |
492 KB |
Output is correct |
43 |
Correct |
789 ms |
620 KB |
Output is correct |
44 |
Correct |
798 ms |
492 KB |
Output is correct |
45 |
Correct |
792 ms |
616 KB |
Output is correct |
46 |
Correct |
758 ms |
620 KB |
Output is correct |
47 |
Correct |
809 ms |
620 KB |
Output is correct |
48 |
Correct |
765 ms |
616 KB |
Output is correct |
49 |
Correct |
768 ms |
492 KB |
Output is correct |
50 |
Correct |
764 ms |
620 KB |
Output is correct |
51 |
Correct |
798 ms |
756 KB |
Output is correct |
52 |
Correct |
793 ms |
492 KB |
Output is correct |
53 |
Correct |
771 ms |
620 KB |
Output is correct |
54 |
Correct |
780 ms |
620 KB |
Output is correct |
55 |
Correct |
766 ms |
616 KB |
Output is correct |