Submission #936813

# Submission time Handle Problem Language Result Execution time Memory
936813 2024-03-02T18:32:21 Z vjudge1 Group Photo (JOI21_ho_t3) C++17
100 / 100
505 ms 349608 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 5007;
const ll INF = 1e18 + 7;
const int logo = 20;
const int off = 1 << logo;

ll inv[MAXN][MAXN], dp[MAXN], c[MAXN][MAXN];
//lesrig[y][x] - koliko brojeva je manje od y nadesno a oni su >= x
int lesrig[MAXN][MAXN], p[MAXN], a[MAXN], arr[MAXN];

ll vl(ll x){
	return (ll)x * (x - 1) / (ll)2;
}

void solve(){
	int n;
	cin >> n;
	for(int i=1; i<=n; i++) cin >> arr[i];

	
	for(int L=1; L<=n; L++){
		int cp = 1;
		for(int i=1; i<=n; i++) if(arr[i] >= L){
			a[cp] = arr[i];
			p[arr[i]] = cp++;
		}
		
		ll su1 = 0, su0 = 0;
		int id = 1;
		for(int R=L; R<=n; R++, id++){
			int cur = a[id];
			if(cur < R) su1 -= p[cur];
			else if(cur > R) su0 += p[cur];
			
			if(cur != R){
				if(p[R] <= id) su0 -= p[R];
				else su1 += p[R];
			}
			
			c[L][R] = su1 - su0;
		}
	}
	
	
	for(int i=0; i<=n; i++) p[i] = 0;
	for(int pos=n; pos; pos--){
		int cur = arr[pos];
		for(int x=1; x<=n; x++) lesrig[cur][x] = p[cur - 1] - p[x - 1];
		for(int i=cur; i<=n; i++) p[i]++;
	}
	
	
	for(int R=1; R<=n; R++){
		for(int L=1; L<R; L++){
			inv[L][R] = inv[L][R - 1] + lesrig[R][L];
		}
	}
	
	dp[0] = 0;
	for(int i=1; i<=n; i++) dp[i] = INF;
	for(int R=1; R<=n; R++){
		for(int L=0; L<R; L++){
			int len = R - L;
			dp[R] = min(dp[R], dp[L] + vl(len) - inv[L + 1][R] + c[L + 1][R]);
		}
	}
	cout << dp[n] << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
	return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 3 ms 15708 KB Output is correct
22 Correct 3 ms 15824 KB Output is correct
23 Correct 3 ms 15708 KB Output is correct
24 Correct 3 ms 15708 KB Output is correct
25 Correct 3 ms 15824 KB Output is correct
26 Correct 4 ms 15708 KB Output is correct
27 Correct 3 ms 15708 KB Output is correct
28 Correct 3 ms 15708 KB Output is correct
29 Correct 3 ms 15708 KB Output is correct
30 Correct 3 ms 15708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 3 ms 15708 KB Output is correct
22 Correct 3 ms 15824 KB Output is correct
23 Correct 3 ms 15708 KB Output is correct
24 Correct 3 ms 15708 KB Output is correct
25 Correct 3 ms 15824 KB Output is correct
26 Correct 4 ms 15708 KB Output is correct
27 Correct 3 ms 15708 KB Output is correct
28 Correct 3 ms 15708 KB Output is correct
29 Correct 3 ms 15708 KB Output is correct
30 Correct 3 ms 15708 KB Output is correct
31 Correct 20 ms 57436 KB Output is correct
32 Correct 16 ms 57436 KB Output is correct
33 Correct 13 ms 57436 KB Output is correct
34 Correct 14 ms 57436 KB Output is correct
35 Correct 13 ms 57436 KB Output is correct
36 Correct 14 ms 57436 KB Output is correct
37 Correct 13 ms 57436 KB Output is correct
38 Correct 14 ms 57436 KB Output is correct
39 Correct 13 ms 57600 KB Output is correct
40 Correct 13 ms 57436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 3 ms 15708 KB Output is correct
22 Correct 3 ms 15824 KB Output is correct
23 Correct 3 ms 15708 KB Output is correct
24 Correct 3 ms 15708 KB Output is correct
25 Correct 3 ms 15824 KB Output is correct
26 Correct 4 ms 15708 KB Output is correct
27 Correct 3 ms 15708 KB Output is correct
28 Correct 3 ms 15708 KB Output is correct
29 Correct 3 ms 15708 KB Output is correct
30 Correct 3 ms 15708 KB Output is correct
31 Correct 20 ms 57436 KB Output is correct
32 Correct 16 ms 57436 KB Output is correct
33 Correct 13 ms 57436 KB Output is correct
34 Correct 14 ms 57436 KB Output is correct
35 Correct 13 ms 57436 KB Output is correct
36 Correct 14 ms 57436 KB Output is correct
37 Correct 13 ms 57436 KB Output is correct
38 Correct 14 ms 57436 KB Output is correct
39 Correct 13 ms 57600 KB Output is correct
40 Correct 13 ms 57436 KB Output is correct
41 Correct 505 ms 349112 KB Output is correct
42 Correct 473 ms 349608 KB Output is correct
43 Correct 470 ms 349436 KB Output is correct
44 Correct 486 ms 349436 KB Output is correct
45 Correct 468 ms 349520 KB Output is correct
46 Correct 467 ms 349504 KB Output is correct
47 Correct 476 ms 349488 KB Output is correct
48 Correct 482 ms 348592 KB Output is correct
49 Correct 469 ms 347848 KB Output is correct
50 Correct 463 ms 348664 KB Output is correct
51 Correct 473 ms 347728 KB Output is correct
52 Correct 481 ms 348680 KB Output is correct
53 Correct 473 ms 347872 KB Output is correct
54 Correct 500 ms 347692 KB Output is correct
55 Correct 473 ms 348924 KB Output is correct