#include <bits/stdc++.h>
#define pb push_back
#define sp " "
#define sl "\n"
#define fi first
#define se second
#define mp make_pair
#define sz(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define rev(a) reverse(a.begin(),a.end())
#define PI 3.141592
#define fo(i,n) for (int i=0;i<n;i++)
#define fofo(i,n,j,m) for (int i=0;i<n;i++) for (int j=0;j<m;j++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N=5e3+5,mod=1e9+7;
int n,a[N];
ll dp[N];
int st[N];
int g[N];
vi aux;
int tam;
void add(int u)
{
while (u<=tam) {
st[u]++;
u += u&(-u);
}
return;
}
int get(int u)
{
int r = 0;
while (u>0) {
r += st[u];
u -= u&(-u);
}
return r;
}
int main() {
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio();
cin >> n;
for (int i=0;i<n;i++) cin >> a[i];
dp[0] = dp[1] = 0;
for (int i=2;i<=n;i++) dp[i] = 1e15;
for (int i=0;i<n;i++) g[a[i]] = i;
for (int i=n;i>=1;i--) {
int p1,p2;
p2 = get(g[i]-1);
}
for (int i=2;i<=n;i++) {
aux.clear();
for (int j=0;j<n;j++) if (a[j]<=i) aux.push_back(a[j]);
tam = i;
int p=0;
long long res = 0;
for (int u : aux) g[u] = ++p;
for (int j=0;j<=n;j++) st[j] = 0;
for (int j=i;j>=1;j--) {
int k = get(g[j]-1);
res += i-g[j]-k;
dp[i] = min(dp[i],dp[j-1]+res);
add(g[j]);
}
//cout << dp[i] << " ";
}
cout << dp[n];
return 0;
}
// Recuerda limites
// Recuerda casos especiales
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:56:7: warning: unused variable 'p1' [-Wunused-variable]
56 | int p1,p2;
| ^~
Main.cpp:56:10: warning: variable 'p2' set but not used [-Wunused-but-set-variable]
56 | int p1,p2;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
296 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
296 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
2 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
296 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
296 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
2 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
10 ms |
332 KB |
Output is correct |
32 |
Correct |
10 ms |
292 KB |
Output is correct |
33 |
Correct |
10 ms |
296 KB |
Output is correct |
34 |
Correct |
11 ms |
332 KB |
Output is correct |
35 |
Correct |
10 ms |
332 KB |
Output is correct |
36 |
Correct |
10 ms |
332 KB |
Output is correct |
37 |
Correct |
10 ms |
332 KB |
Output is correct |
38 |
Correct |
10 ms |
292 KB |
Output is correct |
39 |
Correct |
10 ms |
296 KB |
Output is correct |
40 |
Correct |
10 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
296 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
296 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
2 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
10 ms |
332 KB |
Output is correct |
32 |
Correct |
10 ms |
292 KB |
Output is correct |
33 |
Correct |
10 ms |
296 KB |
Output is correct |
34 |
Correct |
11 ms |
332 KB |
Output is correct |
35 |
Correct |
10 ms |
332 KB |
Output is correct |
36 |
Correct |
10 ms |
332 KB |
Output is correct |
37 |
Correct |
10 ms |
332 KB |
Output is correct |
38 |
Correct |
10 ms |
292 KB |
Output is correct |
39 |
Correct |
10 ms |
296 KB |
Output is correct |
40 |
Correct |
10 ms |
336 KB |
Output is correct |
41 |
Correct |
398 ms |
532 KB |
Output is correct |
42 |
Correct |
395 ms |
476 KB |
Output is correct |
43 |
Correct |
400 ms |
452 KB |
Output is correct |
44 |
Correct |
401 ms |
416 KB |
Output is correct |
45 |
Correct |
399 ms |
452 KB |
Output is correct |
46 |
Correct |
394 ms |
408 KB |
Output is correct |
47 |
Correct |
397 ms |
456 KB |
Output is correct |
48 |
Correct |
395 ms |
412 KB |
Output is correct |
49 |
Correct |
402 ms |
424 KB |
Output is correct |
50 |
Correct |
394 ms |
416 KB |
Output is correct |
51 |
Correct |
395 ms |
452 KB |
Output is correct |
52 |
Correct |
393 ms |
556 KB |
Output is correct |
53 |
Correct |
401 ms |
544 KB |
Output is correct |
54 |
Correct |
396 ms |
580 KB |
Output is correct |
55 |
Correct |
402 ms |
408 KB |
Output is correct |