#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e9 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
struct ABI
{
vector <int> tree;
void update(int i, int v)
{
for(i; i < (int) tree.size(); i += (i & -i))
tree[i] += v;
}
int sum(int i)
{
int ans = 0;
for(i; i > 0; i-= (i & -i))
ans += tree[i];
return ans;
}
int query(int l, int r)
{
if(l <= 1) return sum(r);
else return sum(r) - sum(l-1);
}
ABI(int n)
{
tree.assign(n + 2, 0);
}
};
void solve()
{
int n; cin >> n;
vector <vector<int>> C(n+1, vector <int> (n+1, 0));
vector <int> v(n), mp(n+1), dp(n+1, Inf);
for(auto& u : v) cin >> u;
_for(i, n) mp[v[i]] = i + 1;
for(int i = 1; i <= n; i++) // Costo para invertir los elementos entre [i, j]
{
ABI St(n);
for(int j = i; j <= n; j++)
{
if(j != i) C[i][j] = C[i][j-1] + St.query(1, mp[j]);
St.update(mp[j], 1); //cerr << mp[j] << " " << St.query(1, mp[j]) << endl;
}
}
/*_for(i, n)
{
_for(j, n) cerr << C[i+1][j+1] << " ";
cerr << endl;
} */
ABI St1(n); dp[0] = 0;
for(int i = 0; i < n; i++)
{
int cost = dp[i];
//ABI St2(n);
for(int j = 1; i + j <= n; j++)
{
int x = mp[i+j] + St1.query(mp[i+j], n); // + St2.query(mp[i+j], n)
cost += (x - (i + j));
//cout << j << " " << mp[i+j] << " " << St1.query(mp[i+j], n) << " " << St2.query(mp[i+j], n) << " " << (x - (i + j)) << " " << C[i+1][i+j] << endl;
//cerr << cost <<endl;
dp[i+j] = min(dp[i+j], C[i+1][i+j] + cost);
// St2.update(mp[j], 1);
}
//cerr << dp[n] << endl;
St1.update(mp[i+1], 1); //break;
}
cout << dp[n] << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
solve();
}
return 0;
}
Compilation message
Main.cpp: In member function 'void ABI::update(long long int, long long int)':
Main.cpp:40:13: warning: statement has no effect [-Wunused-value]
40 | for(i; i < (int) tree.size(); i += (i & -i))
| ^
Main.cpp: In member function 'long long int ABI::sum(long long int)':
Main.cpp:47:13: warning: statement has no effect [-Wunused-value]
47 | for(i; i > 0; i-= (i & -i))
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 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 |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 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 |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
756 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
756 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
7 ms |
5468 KB |
Output is correct |
32 |
Correct |
8 ms |
5528 KB |
Output is correct |
33 |
Correct |
8 ms |
5468 KB |
Output is correct |
34 |
Correct |
6 ms |
5468 KB |
Output is correct |
35 |
Correct |
7 ms |
5468 KB |
Output is correct |
36 |
Correct |
7 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5468 KB |
Output is correct |
38 |
Correct |
7 ms |
5540 KB |
Output is correct |
39 |
Correct |
7 ms |
5468 KB |
Output is correct |
40 |
Correct |
8 ms |
5472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
756 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
7 ms |
5468 KB |
Output is correct |
32 |
Correct |
8 ms |
5528 KB |
Output is correct |
33 |
Correct |
8 ms |
5468 KB |
Output is correct |
34 |
Correct |
6 ms |
5468 KB |
Output is correct |
35 |
Correct |
7 ms |
5468 KB |
Output is correct |
36 |
Correct |
7 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5468 KB |
Output is correct |
38 |
Correct |
7 ms |
5540 KB |
Output is correct |
39 |
Correct |
7 ms |
5468 KB |
Output is correct |
40 |
Correct |
8 ms |
5472 KB |
Output is correct |
41 |
Correct |
481 ms |
196352 KB |
Output is correct |
42 |
Correct |
478 ms |
196392 KB |
Output is correct |
43 |
Correct |
483 ms |
196504 KB |
Output is correct |
44 |
Correct |
485 ms |
196500 KB |
Output is correct |
45 |
Correct |
492 ms |
196436 KB |
Output is correct |
46 |
Correct |
485 ms |
196436 KB |
Output is correct |
47 |
Correct |
470 ms |
196544 KB |
Output is correct |
48 |
Correct |
458 ms |
196548 KB |
Output is correct |
49 |
Correct |
456 ms |
196540 KB |
Output is correct |
50 |
Correct |
456 ms |
196436 KB |
Output is correct |
51 |
Correct |
470 ms |
196372 KB |
Output is correct |
52 |
Correct |
462 ms |
196708 KB |
Output is correct |
53 |
Correct |
460 ms |
196536 KB |
Output is correct |
54 |
Correct |
463 ms |
196536 KB |
Output is correct |
55 |
Correct |
455 ms |
196544 KB |
Output is correct |