#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mset multiset
#define F first
#define S second
using namespace std;
int n;
vector<vector<int > > dp(5001, vector<int >(5001, 0));
vector<vector<int > > psum(5001, vector<int >(5001, 0));
int inv(int up, int val, int p)
{
int cnt = 0;
cnt += psum[up][p] - psum[val - 1][p];
cnt += psum[val - 1][n] - psum[val - 1][p];
return cnt;
}
void solve ()
{
cin >> n;
vector<int > arr(n+1), pos(n+1);
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
pos[arr[i]] = i;
}
dp[1][1] = 0;
psum[1][pos[1]] = 1;
for (int i = 1; i <= n; i++) psum[1][i] += psum[1][i-1];
for (int i = 2; i <= n; i++)
{
dp[i][i] = 1e9;
for (int j = 1; j < i; j++)
{
dp[i][j] = dp[i - 1][j] + inv(i - 1, j, pos[i]);
dp[i][i] = min(dp[i][i], dp[i - 1][j] + inv(i - 1, i, pos[i]));
}
for (int j = 1; j <= i; j++) psum[i][pos[j]]++;
for (int j = 1; j <= n; j++) psum[i][j] += psum[i][j - 1];
}
int ans = 1e9;
for (int i = 1; i <= n; i++) ans = min(ans, dp[n][i]);
cout << ans;
}
signed main(){IOS solve(); return 0;}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
392316 KB |
Output is correct |
2 |
Correct |
183 ms |
392256 KB |
Output is correct |
3 |
Correct |
171 ms |
392180 KB |
Output is correct |
4 |
Correct |
179 ms |
392076 KB |
Output is correct |
5 |
Correct |
164 ms |
392280 KB |
Output is correct |
6 |
Correct |
162 ms |
392140 KB |
Output is correct |
7 |
Correct |
164 ms |
392124 KB |
Output is correct |
8 |
Correct |
170 ms |
392272 KB |
Output is correct |
9 |
Correct |
170 ms |
392272 KB |
Output is correct |
10 |
Correct |
168 ms |
392160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
392316 KB |
Output is correct |
2 |
Correct |
183 ms |
392256 KB |
Output is correct |
3 |
Correct |
171 ms |
392180 KB |
Output is correct |
4 |
Correct |
179 ms |
392076 KB |
Output is correct |
5 |
Correct |
164 ms |
392280 KB |
Output is correct |
6 |
Correct |
162 ms |
392140 KB |
Output is correct |
7 |
Correct |
164 ms |
392124 KB |
Output is correct |
8 |
Correct |
170 ms |
392272 KB |
Output is correct |
9 |
Correct |
170 ms |
392272 KB |
Output is correct |
10 |
Correct |
168 ms |
392160 KB |
Output is correct |
11 |
Correct |
166 ms |
392272 KB |
Output is correct |
12 |
Correct |
164 ms |
392220 KB |
Output is correct |
13 |
Correct |
170 ms |
392196 KB |
Output is correct |
14 |
Correct |
169 ms |
392148 KB |
Output is correct |
15 |
Correct |
214 ms |
392268 KB |
Output is correct |
16 |
Correct |
163 ms |
392276 KB |
Output is correct |
17 |
Correct |
209 ms |
392240 KB |
Output is correct |
18 |
Correct |
177 ms |
392272 KB |
Output is correct |
19 |
Correct |
162 ms |
392272 KB |
Output is correct |
20 |
Correct |
167 ms |
392164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
392316 KB |
Output is correct |
2 |
Correct |
183 ms |
392256 KB |
Output is correct |
3 |
Correct |
171 ms |
392180 KB |
Output is correct |
4 |
Correct |
179 ms |
392076 KB |
Output is correct |
5 |
Correct |
164 ms |
392280 KB |
Output is correct |
6 |
Correct |
162 ms |
392140 KB |
Output is correct |
7 |
Correct |
164 ms |
392124 KB |
Output is correct |
8 |
Correct |
170 ms |
392272 KB |
Output is correct |
9 |
Correct |
170 ms |
392272 KB |
Output is correct |
10 |
Correct |
168 ms |
392160 KB |
Output is correct |
11 |
Correct |
166 ms |
392272 KB |
Output is correct |
12 |
Correct |
164 ms |
392220 KB |
Output is correct |
13 |
Correct |
170 ms |
392196 KB |
Output is correct |
14 |
Correct |
169 ms |
392148 KB |
Output is correct |
15 |
Correct |
214 ms |
392268 KB |
Output is correct |
16 |
Correct |
163 ms |
392276 KB |
Output is correct |
17 |
Correct |
209 ms |
392240 KB |
Output is correct |
18 |
Correct |
177 ms |
392272 KB |
Output is correct |
19 |
Correct |
162 ms |
392272 KB |
Output is correct |
20 |
Correct |
167 ms |
392164 KB |
Output is correct |
21 |
Correct |
180 ms |
392192 KB |
Output is correct |
22 |
Correct |
178 ms |
392256 KB |
Output is correct |
23 |
Correct |
176 ms |
392308 KB |
Output is correct |
24 |
Correct |
166 ms |
392348 KB |
Output is correct |
25 |
Correct |
183 ms |
392120 KB |
Output is correct |
26 |
Correct |
165 ms |
392532 KB |
Output is correct |
27 |
Correct |
165 ms |
392216 KB |
Output is correct |
28 |
Correct |
163 ms |
392084 KB |
Output is correct |
29 |
Correct |
176 ms |
392536 KB |
Output is correct |
30 |
Correct |
177 ms |
392160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
392316 KB |
Output is correct |
2 |
Correct |
183 ms |
392256 KB |
Output is correct |
3 |
Correct |
171 ms |
392180 KB |
Output is correct |
4 |
Correct |
179 ms |
392076 KB |
Output is correct |
5 |
Correct |
164 ms |
392280 KB |
Output is correct |
6 |
Correct |
162 ms |
392140 KB |
Output is correct |
7 |
Correct |
164 ms |
392124 KB |
Output is correct |
8 |
Correct |
170 ms |
392272 KB |
Output is correct |
9 |
Correct |
170 ms |
392272 KB |
Output is correct |
10 |
Correct |
168 ms |
392160 KB |
Output is correct |
11 |
Correct |
166 ms |
392272 KB |
Output is correct |
12 |
Correct |
164 ms |
392220 KB |
Output is correct |
13 |
Correct |
170 ms |
392196 KB |
Output is correct |
14 |
Correct |
169 ms |
392148 KB |
Output is correct |
15 |
Correct |
214 ms |
392268 KB |
Output is correct |
16 |
Correct |
163 ms |
392276 KB |
Output is correct |
17 |
Correct |
209 ms |
392240 KB |
Output is correct |
18 |
Correct |
177 ms |
392272 KB |
Output is correct |
19 |
Correct |
162 ms |
392272 KB |
Output is correct |
20 |
Correct |
167 ms |
392164 KB |
Output is correct |
21 |
Correct |
180 ms |
392192 KB |
Output is correct |
22 |
Correct |
178 ms |
392256 KB |
Output is correct |
23 |
Correct |
176 ms |
392308 KB |
Output is correct |
24 |
Correct |
166 ms |
392348 KB |
Output is correct |
25 |
Correct |
183 ms |
392120 KB |
Output is correct |
26 |
Correct |
165 ms |
392532 KB |
Output is correct |
27 |
Correct |
165 ms |
392216 KB |
Output is correct |
28 |
Correct |
163 ms |
392084 KB |
Output is correct |
29 |
Correct |
176 ms |
392536 KB |
Output is correct |
30 |
Correct |
177 ms |
392160 KB |
Output is correct |
31 |
Correct |
189 ms |
392276 KB |
Output is correct |
32 |
Correct |
178 ms |
392196 KB |
Output is correct |
33 |
Correct |
169 ms |
392088 KB |
Output is correct |
34 |
Correct |
182 ms |
392292 KB |
Output is correct |
35 |
Correct |
170 ms |
392528 KB |
Output is correct |
36 |
Correct |
169 ms |
392204 KB |
Output is correct |
37 |
Correct |
174 ms |
392276 KB |
Output is correct |
38 |
Correct |
173 ms |
392280 KB |
Output is correct |
39 |
Correct |
173 ms |
392268 KB |
Output is correct |
40 |
Correct |
172 ms |
392100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
392316 KB |
Output is correct |
2 |
Correct |
183 ms |
392256 KB |
Output is correct |
3 |
Correct |
171 ms |
392180 KB |
Output is correct |
4 |
Correct |
179 ms |
392076 KB |
Output is correct |
5 |
Correct |
164 ms |
392280 KB |
Output is correct |
6 |
Correct |
162 ms |
392140 KB |
Output is correct |
7 |
Correct |
164 ms |
392124 KB |
Output is correct |
8 |
Correct |
170 ms |
392272 KB |
Output is correct |
9 |
Correct |
170 ms |
392272 KB |
Output is correct |
10 |
Correct |
168 ms |
392160 KB |
Output is correct |
11 |
Correct |
166 ms |
392272 KB |
Output is correct |
12 |
Correct |
164 ms |
392220 KB |
Output is correct |
13 |
Correct |
170 ms |
392196 KB |
Output is correct |
14 |
Correct |
169 ms |
392148 KB |
Output is correct |
15 |
Correct |
214 ms |
392268 KB |
Output is correct |
16 |
Correct |
163 ms |
392276 KB |
Output is correct |
17 |
Correct |
209 ms |
392240 KB |
Output is correct |
18 |
Correct |
177 ms |
392272 KB |
Output is correct |
19 |
Correct |
162 ms |
392272 KB |
Output is correct |
20 |
Correct |
167 ms |
392164 KB |
Output is correct |
21 |
Correct |
180 ms |
392192 KB |
Output is correct |
22 |
Correct |
178 ms |
392256 KB |
Output is correct |
23 |
Correct |
176 ms |
392308 KB |
Output is correct |
24 |
Correct |
166 ms |
392348 KB |
Output is correct |
25 |
Correct |
183 ms |
392120 KB |
Output is correct |
26 |
Correct |
165 ms |
392532 KB |
Output is correct |
27 |
Correct |
165 ms |
392216 KB |
Output is correct |
28 |
Correct |
163 ms |
392084 KB |
Output is correct |
29 |
Correct |
176 ms |
392536 KB |
Output is correct |
30 |
Correct |
177 ms |
392160 KB |
Output is correct |
31 |
Correct |
189 ms |
392276 KB |
Output is correct |
32 |
Correct |
178 ms |
392196 KB |
Output is correct |
33 |
Correct |
169 ms |
392088 KB |
Output is correct |
34 |
Correct |
182 ms |
392292 KB |
Output is correct |
35 |
Correct |
170 ms |
392528 KB |
Output is correct |
36 |
Correct |
169 ms |
392204 KB |
Output is correct |
37 |
Correct |
174 ms |
392276 KB |
Output is correct |
38 |
Correct |
173 ms |
392280 KB |
Output is correct |
39 |
Correct |
173 ms |
392268 KB |
Output is correct |
40 |
Correct |
172 ms |
392100 KB |
Output is correct |
41 |
Correct |
741 ms |
392416 KB |
Output is correct |
42 |
Correct |
748 ms |
392532 KB |
Output is correct |
43 |
Correct |
762 ms |
392412 KB |
Output is correct |
44 |
Correct |
732 ms |
392420 KB |
Output is correct |
45 |
Correct |
758 ms |
392416 KB |
Output is correct |
46 |
Correct |
739 ms |
392420 KB |
Output is correct |
47 |
Correct |
762 ms |
392416 KB |
Output is correct |
48 |
Correct |
785 ms |
392268 KB |
Output is correct |
49 |
Correct |
739 ms |
392652 KB |
Output is correct |
50 |
Correct |
740 ms |
392456 KB |
Output is correct |
51 |
Correct |
784 ms |
392420 KB |
Output is correct |
52 |
Correct |
784 ms |
392412 KB |
Output is correct |
53 |
Correct |
795 ms |
392412 KB |
Output is correct |
54 |
Correct |
799 ms |
392412 KB |
Output is correct |
55 |
Correct |
729 ms |
392412 KB |
Output is correct |