Submission #891815

# Submission time Handle Problem Language Result Execution time Memory
891815 2023-12-24T06:04:54 Z LCJLY Group Photo (JOI21_ho_t3) C++14
64 / 100
5000 ms 196904 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

int prefix[5005][5005]; //pos val
int arr[5005];
int pos[5005];
int n=0;

int query(int l, int r, int val, int val2){
	if(val2<val) return 0;
	if(r<l) return 0; 
	int hold= prefix[r][val2]-prefix[r][val-1]-prefix[l-1][val2]+prefix[l-1][val-1];
	//show2(l,l,r,r);
	//show3(val,val,val2,val2,hold,hold);
	return hold;
}

int memo[5005];

int dp(int index){
	if(index==n+1) return 0;
	if(memo[index]!=-1) return memo[index];
	int ans=INT_MAX;
	
	for(int x=index;x<=n;x++){
		int cost=0;
		for(int y=index;y<=x;y++){
			//outside
			cost+=query(1,pos[y],x+1,n);
			//inside inversion
			cost+=query(1,pos[y],index,y-1);
			//show3(y,y,pos[y],pos[y],cost,cost);
		}
		int take=dp(x+1);
		//show3(index,index,x,x,cost,cost);
		//show(take,take);
		ans=min(ans,take+cost);
	}
	return memo[index]=ans;
}

void solve(){
	cin >> n;
	
	for(int x=1;x<=n;x++){
		cin >> arr[x];
		prefix[x][arr[x]]=1;
		pos[arr[x]]=x;
	}
	
	for(int x=1;x<=n;x++){
		for(int y=1;y<=n;y++){
			prefix[x][y]+=prefix[x-1][y]+prefix[x][y-1]-prefix[x-1][y-1];
		}
	}
	
	memset(memo,-1,sizeof(memo));
	cout << dp(1);
	
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}



		


		
		
	
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 8536 KB Output is correct
22 Correct 5 ms 8540 KB Output is correct
23 Correct 5 ms 8540 KB Output is correct
24 Correct 5 ms 8536 KB Output is correct
25 Correct 5 ms 8656 KB Output is correct
26 Correct 5 ms 8540 KB Output is correct
27 Correct 5 ms 8540 KB Output is correct
28 Correct 5 ms 8540 KB Output is correct
29 Correct 5 ms 8540 KB Output is correct
30 Correct 5 ms 8736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 8536 KB Output is correct
22 Correct 5 ms 8540 KB Output is correct
23 Correct 5 ms 8540 KB Output is correct
24 Correct 5 ms 8536 KB Output is correct
25 Correct 5 ms 8656 KB Output is correct
26 Correct 5 ms 8540 KB Output is correct
27 Correct 5 ms 8540 KB Output is correct
28 Correct 5 ms 8540 KB Output is correct
29 Correct 5 ms 8540 KB Output is correct
30 Correct 5 ms 8736 KB Output is correct
31 Correct 260 ms 33368 KB Output is correct
32 Correct 281 ms 33620 KB Output is correct
33 Correct 283 ms 33412 KB Output is correct
34 Correct 268 ms 33368 KB Output is correct
35 Correct 266 ms 33416 KB Output is correct
36 Correct 284 ms 33372 KB Output is correct
37 Correct 277 ms 33624 KB Output is correct
38 Correct 265 ms 33368 KB Output is correct
39 Correct 265 ms 33368 KB Output is correct
40 Correct 281 ms 33416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 8536 KB Output is correct
22 Correct 5 ms 8540 KB Output is correct
23 Correct 5 ms 8540 KB Output is correct
24 Correct 5 ms 8536 KB Output is correct
25 Correct 5 ms 8656 KB Output is correct
26 Correct 5 ms 8540 KB Output is correct
27 Correct 5 ms 8540 KB Output is correct
28 Correct 5 ms 8540 KB Output is correct
29 Correct 5 ms 8540 KB Output is correct
30 Correct 5 ms 8736 KB Output is correct
31 Correct 260 ms 33368 KB Output is correct
32 Correct 281 ms 33620 KB Output is correct
33 Correct 283 ms 33412 KB Output is correct
34 Correct 268 ms 33368 KB Output is correct
35 Correct 266 ms 33416 KB Output is correct
36 Correct 284 ms 33372 KB Output is correct
37 Correct 277 ms 33624 KB Output is correct
38 Correct 265 ms 33368 KB Output is correct
39 Correct 265 ms 33368 KB Output is correct
40 Correct 281 ms 33416 KB Output is correct
41 Execution timed out 5058 ms 196904 KB Time limit exceeded
42 Halted 0 ms 0 KB -