#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define vec vector
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll mod = 1e9 + 7;
ld fpow(ll a, ll b, ll m = -1)
{
if (m != -1)
a %= m;
ll res = 1;
while (b > 0)
{
if (b % 2 == 1)
{
if (m != -1)
res = res * a % m;
else
{
res *= a;
}
}
a = a * a;
if (m != -1)
{
a %= m;
}
b /= 2;
}
return res;
}
int main()
{
// fast;
// freopen("pails.in", "r", stdin);
// freopen("pails.out", "w", stdout);
ll t = 1;
// cin >> t;
while (t--)
{
ll n;
cin >> n;
vec<ll> v(n), a(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
v[i]--;
a[v[i]] = i;
}
vec<vec<ll>> b(n, vec<ll>(n+1)), c(n, vec<ll>(n+1)), d(n, vec<ll>(n+1));
ordered_set os;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[v[i]][j] = os.order_of_key(j);
}
os.insert(v[i]);
}
// b[i][j] = >j
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
c[i][j] = b[i][i] - b[i][j];
// cout << c[i][j] << ';';
}
// cout << '\n';
}
//c[i][j] = <i&>=j
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
b[i][j] = a[i] - b[i][j];
// cout << b[i][j] << ' ';
}
// cout << '\n';
}
// cout << '\n';
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n; j++)
b[j][i] += b[j - 1][i], c[j][i]+=c[j-1][i];
}
// for (int i = 0; i < n; i++)
{
// for (int j = 0; j < n; j++){
// cout << b[i][j] << ' ';}
// cout << '\n';
}
vec<ll> dp(n);
for (int i = 0; i < n; i++)
{
dp[i] = b[i][i+1]+c[i][0];
for (int j = 0; j < i; j++)
{
ll x = b[i][i+1] - (b[j][i+1])+c[i][j+1];
dp[i] = min(dp[i], dp[j] + x);
// cout << x << ';';
}
// cout << dp[i] << ' ';
}
cout << dp[n - 1];
end:;
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:112:2: warning: label 'end' defined but not used [-Wunused-label]
112 | end:;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
31 |
Correct |
19 ms |
15448 KB |
Output is correct |
32 |
Correct |
21 ms |
15452 KB |
Output is correct |
33 |
Correct |
18 ms |
15448 KB |
Output is correct |
34 |
Correct |
18 ms |
15620 KB |
Output is correct |
35 |
Correct |
19 ms |
15448 KB |
Output is correct |
36 |
Correct |
20 ms |
15452 KB |
Output is correct |
37 |
Correct |
17 ms |
15620 KB |
Output is correct |
38 |
Correct |
17 ms |
15452 KB |
Output is correct |
39 |
Correct |
20 ms |
15452 KB |
Output is correct |
40 |
Correct |
19 ms |
15452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
31 |
Correct |
19 ms |
15448 KB |
Output is correct |
32 |
Correct |
21 ms |
15452 KB |
Output is correct |
33 |
Correct |
18 ms |
15448 KB |
Output is correct |
34 |
Correct |
18 ms |
15620 KB |
Output is correct |
35 |
Correct |
19 ms |
15448 KB |
Output is correct |
36 |
Correct |
20 ms |
15452 KB |
Output is correct |
37 |
Correct |
17 ms |
15620 KB |
Output is correct |
38 |
Correct |
17 ms |
15452 KB |
Output is correct |
39 |
Correct |
20 ms |
15452 KB |
Output is correct |
40 |
Correct |
19 ms |
15452 KB |
Output is correct |
41 |
Correct |
1482 ms |
587752 KB |
Output is correct |
42 |
Correct |
1532 ms |
587856 KB |
Output is correct |
43 |
Correct |
1569 ms |
588372 KB |
Output is correct |
44 |
Correct |
1505 ms |
588260 KB |
Output is correct |
45 |
Correct |
1519 ms |
588276 KB |
Output is correct |
46 |
Correct |
1499 ms |
588408 KB |
Output is correct |
47 |
Correct |
1449 ms |
588484 KB |
Output is correct |
48 |
Correct |
1432 ms |
588368 KB |
Output is correct |
49 |
Correct |
1524 ms |
588368 KB |
Output is correct |
50 |
Correct |
1472 ms |
588512 KB |
Output is correct |
51 |
Correct |
1374 ms |
588532 KB |
Output is correct |
52 |
Correct |
1510 ms |
588368 KB |
Output is correct |
53 |
Correct |
1531 ms |
588460 KB |
Output is correct |
54 |
Correct |
1479 ms |
588368 KB |
Output is correct |
55 |
Correct |
1399 ms |
588368 KB |
Output is correct |