Submission #538498

# Submission time Handle Problem Language Result Execution time Memory
538498 2022-03-17T03:19:56 Z Haruto810198 Group Photo (JOI21_ho_t3) C++17
100 / 100
999 ms 509808 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double

#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())

#define vi vector<int>
#define pii pair<int, int>

#define F first
#define S second

#define pb push_back
#define eb emplace_back
#define mkp make_pair

const int INF = INT_MAX;
const int LNF = INF*INF;
const int MOD = 1000000007;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

const int MAX = 5010;

int n;
int arr[MAX][MAX]; // arr[i] = arr. after sorting [1, i]
int pos[MAX][MAX];
int cost[MAX][MAX]; // cost[i][j] = cost to sort [i, j]
int dp[MAX];
int res;

inline int sum(int l, int r){
	return (l + r) * (r - l + 1) / 2;
}

#define lsb(x) ((x)&(-(x)))

int BIT[MAX];

void modify(int pos, int val){
	while(pos < MAX){
		BIT[pos] += val;
		pos += lsb(pos);
	}
}

int query(int pos){
	int ret = 0;
	while(pos > 0){
		ret += BIT[pos];
		pos -= lsb(pos);
	}
	return ret;
}

signed main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n;
	FOR(i, 1, n, 1){
		cin>>arr[0][i];
	}

	FOR(i, 1, n, 1){
		int ptr;
		FOR(j, 1, n, 1){
			arr[i][j] = arr[i-1][j];
			if(arr[i][j] == i) ptr = j;
		}

		while(ptr > i){
			swap(arr[i][ptr-1], arr[i][ptr]);
			ptr--;
		}
	}
	
	FOR(i, 0, n, 1){
		FOR(j, 1, n, 1){
			pos[i][ arr[i][j] ] = j;
		}
	}
	
	FOR(l, 1, n, 1){
		int dpos = 0;
		int inv = 0;
		FOR(r, l, n, 1){
			// cost[l, r]

			dpos += pos[l-1][r] - r;
			inv += query(pos[l-1][r] - 1);
			modify(pos[l-1][r], 1);

			cost[l][r] = dpos + inv;
		}
		
		FOR(r, l, n, 1){
			modify(pos[l-1][r], -1);
		}
	}

	FOR(r, 1, n, 1){
		dp[r] = LNF;
		FOR(l, 1, r, 1){
			dp[r] = min(dp[r], dp[l-1] + cost[l][r]);
		}
	}

	cout<<dp[n]<<'\n';
	/*
	FOR(l, 1, n, 1){
		cerr<<"cost["<<l<<"] : ";
		FOR(r, 1, n, 1){
			cerr<<cost[l][r]<<" ";
		}
		cerr<<endl;
	}
	cerr<<endl;
	*/
	return 0;

}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:73:7: warning: 'ptr' may be used uninitialized in this function [-Wmaybe-uninitialized]
   73 |   int ptr;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 624 KB Output is correct
21 Correct 4 ms 3540 KB Output is correct
22 Correct 3 ms 3540 KB Output is correct
23 Correct 3 ms 3592 KB Output is correct
24 Correct 3 ms 3540 KB Output is correct
25 Correct 3 ms 3540 KB Output is correct
26 Correct 3 ms 3540 KB Output is correct
27 Correct 3 ms 3532 KB Output is correct
28 Correct 3 ms 3540 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 624 KB Output is correct
21 Correct 4 ms 3540 KB Output is correct
22 Correct 3 ms 3540 KB Output is correct
23 Correct 3 ms 3592 KB Output is correct
24 Correct 3 ms 3540 KB Output is correct
25 Correct 3 ms 3540 KB Output is correct
26 Correct 3 ms 3540 KB Output is correct
27 Correct 3 ms 3532 KB Output is correct
28 Correct 3 ms 3540 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3524 KB Output is correct
31 Correct 26 ms 22480 KB Output is correct
32 Correct 25 ms 22560 KB Output is correct
33 Correct 28 ms 22584 KB Output is correct
34 Correct 28 ms 22604 KB Output is correct
35 Correct 24 ms 22588 KB Output is correct
36 Correct 24 ms 22640 KB Output is correct
37 Correct 26 ms 22736 KB Output is correct
38 Correct 25 ms 22596 KB Output is correct
39 Correct 26 ms 22540 KB Output is correct
40 Correct 29 ms 22648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 624 KB Output is correct
21 Correct 4 ms 3540 KB Output is correct
22 Correct 3 ms 3540 KB Output is correct
23 Correct 3 ms 3592 KB Output is correct
24 Correct 3 ms 3540 KB Output is correct
25 Correct 3 ms 3540 KB Output is correct
26 Correct 3 ms 3540 KB Output is correct
27 Correct 3 ms 3532 KB Output is correct
28 Correct 3 ms 3540 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3524 KB Output is correct
31 Correct 26 ms 22480 KB Output is correct
32 Correct 25 ms 22560 KB Output is correct
33 Correct 28 ms 22584 KB Output is correct
34 Correct 28 ms 22604 KB Output is correct
35 Correct 24 ms 22588 KB Output is correct
36 Correct 24 ms 22640 KB Output is correct
37 Correct 26 ms 22736 KB Output is correct
38 Correct 25 ms 22596 KB Output is correct
39 Correct 26 ms 22540 KB Output is correct
40 Correct 29 ms 22648 KB Output is correct
41 Correct 999 ms 509260 KB Output is correct
42 Correct 911 ms 509496 KB Output is correct
43 Correct 894 ms 509704 KB Output is correct
44 Correct 906 ms 509720 KB Output is correct
45 Correct 879 ms 509632 KB Output is correct
46 Correct 920 ms 509716 KB Output is correct
47 Correct 881 ms 509776 KB Output is correct
48 Correct 921 ms 509808 KB Output is correct
49 Correct 911 ms 509616 KB Output is correct
50 Correct 876 ms 509704 KB Output is correct
51 Correct 910 ms 509636 KB Output is correct
52 Correct 883 ms 509728 KB Output is correct
53 Correct 888 ms 509716 KB Output is correct
54 Correct 900 ms 509744 KB Output is correct
55 Correct 937 ms 509732 KB Output is correct