#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out() { cerr << endl; }
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cerr << ' ' << A; debug_out(B...); }
template<typename T> void mdebug_out(T* a, int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; }
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) { for (auto x : v) stream << x << ' '; return stream; }
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) { return stream << p.first << ' ' << p.second; }
const int N = 5003;
int P[N], iP[N];
int sum[N][N];
int dp[N];
int gt(int x1, int x2, int y1, int y2)
{
if (x1 > x2 || y1 > y2) return 0;
--x1;
--y1;
return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1];
}
signed main()
{
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> P[i];
iP[P[i]] = i;
}
for (int l = 1; l <= n; ++l)
for (int r = 1; r <= n; ++r)
if (iP[l] > iP[r])
sum[l][r] = 1;
for (int x = 0; x <= n; ++x)
for (int y = 1; y <= n; ++y)
sum[x][y] += sum[x][y - 1];
for (int y = 0; y <= n; ++y)
for (int x = 1; x <= n; ++x)
sum[x][y] += sum[x - 1][y];
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
int g = 0;
for (int p = i - 1; p >= 0; --p)
{
int x = i - p;
g += gt(p + 2, p + x, p + 1, p + 1);
chkmin(dp[i], dp[p] + gt(1, p, p + 1, p + x) + g);
}
}
cout << dp[n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1388 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
3 ms |
1388 KB |
Output is correct |
25 |
Correct |
2 ms |
1388 KB |
Output is correct |
26 |
Correct |
2 ms |
1388 KB |
Output is correct |
27 |
Correct |
2 ms |
1388 KB |
Output is correct |
28 |
Correct |
2 ms |
1388 KB |
Output is correct |
29 |
Correct |
2 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1388 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
3 ms |
1388 KB |
Output is correct |
25 |
Correct |
2 ms |
1388 KB |
Output is correct |
26 |
Correct |
2 ms |
1388 KB |
Output is correct |
27 |
Correct |
2 ms |
1388 KB |
Output is correct |
28 |
Correct |
2 ms |
1388 KB |
Output is correct |
29 |
Correct |
2 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
1388 KB |
Output is correct |
31 |
Correct |
11 ms |
6124 KB |
Output is correct |
32 |
Correct |
12 ms |
6124 KB |
Output is correct |
33 |
Correct |
13 ms |
6252 KB |
Output is correct |
34 |
Correct |
14 ms |
6124 KB |
Output is correct |
35 |
Correct |
11 ms |
6124 KB |
Output is correct |
36 |
Correct |
14 ms |
6124 KB |
Output is correct |
37 |
Correct |
12 ms |
6124 KB |
Output is correct |
38 |
Correct |
11 ms |
6124 KB |
Output is correct |
39 |
Correct |
11 ms |
6124 KB |
Output is correct |
40 |
Correct |
11 ms |
6124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1388 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
3 ms |
1388 KB |
Output is correct |
25 |
Correct |
2 ms |
1388 KB |
Output is correct |
26 |
Correct |
2 ms |
1388 KB |
Output is correct |
27 |
Correct |
2 ms |
1388 KB |
Output is correct |
28 |
Correct |
2 ms |
1388 KB |
Output is correct |
29 |
Correct |
2 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
1388 KB |
Output is correct |
31 |
Correct |
11 ms |
6124 KB |
Output is correct |
32 |
Correct |
12 ms |
6124 KB |
Output is correct |
33 |
Correct |
13 ms |
6252 KB |
Output is correct |
34 |
Correct |
14 ms |
6124 KB |
Output is correct |
35 |
Correct |
11 ms |
6124 KB |
Output is correct |
36 |
Correct |
14 ms |
6124 KB |
Output is correct |
37 |
Correct |
12 ms |
6124 KB |
Output is correct |
38 |
Correct |
11 ms |
6124 KB |
Output is correct |
39 |
Correct |
11 ms |
6124 KB |
Output is correct |
40 |
Correct |
11 ms |
6124 KB |
Output is correct |
41 |
Correct |
822 ms |
98348 KB |
Output is correct |
42 |
Correct |
806 ms |
98480 KB |
Output is correct |
43 |
Correct |
801 ms |
98284 KB |
Output is correct |
44 |
Correct |
822 ms |
98412 KB |
Output is correct |
45 |
Correct |
823 ms |
98412 KB |
Output is correct |
46 |
Correct |
835 ms |
98408 KB |
Output is correct |
47 |
Correct |
804 ms |
98284 KB |
Output is correct |
48 |
Correct |
825 ms |
98396 KB |
Output is correct |
49 |
Correct |
827 ms |
98400 KB |
Output is correct |
50 |
Correct |
818 ms |
98404 KB |
Output is correct |
51 |
Correct |
826 ms |
98388 KB |
Output is correct |
52 |
Correct |
833 ms |
98516 KB |
Output is correct |
53 |
Correct |
844 ms |
98284 KB |
Output is correct |
54 |
Correct |
830 ms |
98304 KB |
Output is correct |
55 |
Correct |
850 ms |
98412 KB |
Output is correct |