답안 #942035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942035 2024-03-10T04:13:10 Z pcc Group Photo (JOI21_ho_t3) C++14
100 / 100
894 ms 77752 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>


const int mxn = 5050;
int N;
int arr[mxn],pos[mxn];
int aux[mxn][mxn];
ll bit1[mxn],bit2[mxn];
int dp[mxn];

void modify(ll bit[],int p,int v){
	for(;p<mxn;p+=p&-p)bit[p] += v;
	return;
}
int getval(ll bit[],int s,int e){
	int re = 0;
	for(;e>0;e-= e&-e)re += bit[e];
	s--;
	for(;s>0;s-= s&-s)re -= bit[s];
	return re;
}

void calc(){
	fill(dp,dp+mxn,mxn*mxn);
	dp[0] = 0;
	for(int i = 0;i<=N;i++){
		memset(bit1,0,sizeof(bit1));
		memset(bit2,0,sizeof(bit2));
		int sh = 0;
		for(int j = 1;j<=N;j++){
			if(j>i)modify(bit1,pos[j],1);
		}
		for(int j = i+1;j<=N;j++){
			int tdp = dp[i]+aux[i+1][j];
			sh = sh+getval(bit1,0,pos[j]-1)-getval(bit2,pos[j]+1,N);
			modify(bit1,pos[j],-1);
			modify(bit2,pos[j],1);
			dp[j] = min(dp[j],tdp+sh);
			//cerr<<i<<' '<<j<<' '<<tdp<<":"<<sh<<endl;
		}
	}
	//for(int i = 1;i<=N;i++)cerr<<dp[i]<<' ';cerr<<endl;
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N;
	for(int i = 1;i<=N;i++)cin>>arr[i],pos[arr[i]] = i;
	for(int s = 1;s<=N;s++){
		memset(bit1,0,sizeof(bit1));
		for(int e = s;e<=N;e++){
			aux[s][e] = aux[s][e-1];
			aux[s][e] += getval(bit1,0,pos[e]);
			modify(bit1,pos[e],1);
		}
	}
	calc();
	cout<<dp[N];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 2 ms 1368 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 2 ms 1340 KB Output is correct
24 Correct 3 ms 1528 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 2 ms 1368 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 2 ms 1340 KB Output is correct
24 Correct 3 ms 1528 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 24 ms 6236 KB Output is correct
32 Correct 24 ms 6436 KB Output is correct
33 Correct 21 ms 6232 KB Output is correct
34 Correct 17 ms 6236 KB Output is correct
35 Correct 17 ms 6236 KB Output is correct
36 Correct 17 ms 6232 KB Output is correct
37 Correct 18 ms 6236 KB Output is correct
38 Correct 18 ms 6444 KB Output is correct
39 Correct 18 ms 6452 KB Output is correct
40 Correct 17 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 2 ms 1368 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 2 ms 1340 KB Output is correct
24 Correct 3 ms 1528 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 24 ms 6236 KB Output is correct
32 Correct 24 ms 6436 KB Output is correct
33 Correct 21 ms 6232 KB Output is correct
34 Correct 17 ms 6236 KB Output is correct
35 Correct 17 ms 6236 KB Output is correct
36 Correct 17 ms 6232 KB Output is correct
37 Correct 18 ms 6236 KB Output is correct
38 Correct 18 ms 6444 KB Output is correct
39 Correct 18 ms 6452 KB Output is correct
40 Correct 17 ms 6236 KB Output is correct
41 Correct 872 ms 77712 KB Output is correct
42 Correct 862 ms 77696 KB Output is correct
43 Correct 894 ms 77180 KB Output is correct
44 Correct 872 ms 77672 KB Output is correct
45 Correct 871 ms 76932 KB Output is correct
46 Correct 881 ms 77684 KB Output is correct
47 Correct 860 ms 77688 KB Output is correct
48 Correct 855 ms 77752 KB Output is correct
49 Correct 865 ms 77748 KB Output is correct
50 Correct 847 ms 77652 KB Output is correct
51 Correct 835 ms 77652 KB Output is correct
52 Correct 833 ms 77748 KB Output is correct
53 Correct 844 ms 77752 KB Output is correct
54 Correct 843 ms 77748 KB Output is correct
55 Correct 838 ms 77752 KB Output is correct