# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
499858 |
2021-12-29T20:11:48 Z |
Lobo |
Group Photo (JOI21_ho_t3) |
C++17 |
|
703 ms |
200616 KB |
#include<bits/stdc++.h>
using namespace std;
/*for ordered_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
*/
const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 5100
ii n, a[maxn], pos[maxn], dp[maxn], mark[maxn];
ii bit[maxn][maxn], bit1[maxn][maxn];
void att(ii id1, ii id2, ii val) {
for(ii i = id2; i <= n; i+= i&-i) {
bit[id1][i]+= val;
}
}
ii qr(ii id1, ii id2) {
ii val = 0;
for(ii i = id2; i > 0; i-= i&-i) {
val+= bit[id1][i];
}
return val;
}
void att1(ii id1, ii id2, ii val) {
for(ii i = id2; i <= n; i+= i&-i) {
bit1[id1][i]+= val;
}
}
ii qr1(ii id1, ii id2) {
ii val = 0;
for(ii i = id2; i > 0; i-= i&-i) {
val+= bit1[id1][i];
}
return val;
}
ii sol(ii i) {
if(mark[i]) return dp[i];
mark[i] = 1;
ii ans = INFii;
if(i == n) return dp[i] = 0;
for(ii j = 1; j <= n; j++) {
bit[i][j] = 0;
bit1[i][j] = 0;
}
for(ii j = 1; j <= i; j++) {
att(i,pos[j],1);
}
ii ans1 = 0;
for(ii j = i+1; j <= n; j++) {
//colocando o j com os <= i
ans1+= i-qr(i,pos[j]);
//atualizando a parte do j -> j+1
ans1+= j-i-1;
//colocando a mudanca pos[x] > pos[j]
ans1-= j-i-1-qr1(i,pos[j]);
att1(i,pos[j],1);
ans = min(ans, ans1+sol(j));
// cout << i << " " << j << " " << ans1 << " " << sol(j) << endl;
}
return dp[i] = ans;
}
void solve() {
cin >> n;
for(ii i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
cout << sol(0) << endl;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
ii tt = 1;
// cin >> tt;
while(tt--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
2124 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
2252 KB |
Output is correct |
26 |
Correct |
1 ms |
2252 KB |
Output is correct |
27 |
Correct |
1 ms |
2252 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
2124 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
2252 KB |
Output is correct |
26 |
Correct |
1 ms |
2252 KB |
Output is correct |
27 |
Correct |
1 ms |
2252 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
17 ms |
11812 KB |
Output is correct |
32 |
Correct |
19 ms |
11712 KB |
Output is correct |
33 |
Correct |
17 ms |
11780 KB |
Output is correct |
34 |
Correct |
17 ms |
11872 KB |
Output is correct |
35 |
Correct |
16 ms |
11772 KB |
Output is correct |
36 |
Correct |
16 ms |
11852 KB |
Output is correct |
37 |
Correct |
17 ms |
11868 KB |
Output is correct |
38 |
Correct |
17 ms |
11868 KB |
Output is correct |
39 |
Correct |
16 ms |
11852 KB |
Output is correct |
40 |
Correct |
19 ms |
11872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
2124 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
2252 KB |
Output is correct |
26 |
Correct |
1 ms |
2252 KB |
Output is correct |
27 |
Correct |
1 ms |
2252 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
17 ms |
11812 KB |
Output is correct |
32 |
Correct |
19 ms |
11712 KB |
Output is correct |
33 |
Correct |
17 ms |
11780 KB |
Output is correct |
34 |
Correct |
17 ms |
11872 KB |
Output is correct |
35 |
Correct |
16 ms |
11772 KB |
Output is correct |
36 |
Correct |
16 ms |
11852 KB |
Output is correct |
37 |
Correct |
17 ms |
11868 KB |
Output is correct |
38 |
Correct |
17 ms |
11868 KB |
Output is correct |
39 |
Correct |
16 ms |
11852 KB |
Output is correct |
40 |
Correct |
19 ms |
11872 KB |
Output is correct |
41 |
Correct |
693 ms |
200252 KB |
Output is correct |
42 |
Correct |
696 ms |
200408 KB |
Output is correct |
43 |
Correct |
690 ms |
200568 KB |
Output is correct |
44 |
Correct |
692 ms |
200536 KB |
Output is correct |
45 |
Correct |
702 ms |
200336 KB |
Output is correct |
46 |
Correct |
685 ms |
200488 KB |
Output is correct |
47 |
Correct |
701 ms |
200444 KB |
Output is correct |
48 |
Correct |
698 ms |
200616 KB |
Output is correct |
49 |
Correct |
690 ms |
200516 KB |
Output is correct |
50 |
Correct |
692 ms |
200516 KB |
Output is correct |
51 |
Correct |
703 ms |
200480 KB |
Output is correct |
52 |
Correct |
685 ms |
200480 KB |
Output is correct |
53 |
Correct |
693 ms |
200480 KB |
Output is correct |
54 |
Correct |
699 ms |
200368 KB |
Output is correct |
55 |
Correct |
690 ms |
200612 KB |
Output is correct |