답안 #590306

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
590306 2022-07-05T19:26:52 Z penguinhacker Boarding Passes (BOI22_passes) C++17
100 / 100
105 ms 26600 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=1e5, BIT=15;
int n, pref[mxN+1][BIT];
string s;
ll dp[1<<BIT], first[BIT][BIT]; // groups a, b: how many i in group a and j in group b such that i<j
vector<int> oc[BIT], change[BIT][BIT]; // groups a, b: change in taking one more from the front than in the back
vector<ll> change_sum[BIT][BIT];

// we are considering adding group a after b is already all seated

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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> s, n=s.size();
	for (int i=0; i<n; ++i) {
		++pref[i+1][s[i]-'A'];
		oc[s[i]-'A'].push_back(i);
	}
	for (int i=1; i<=n; ++i)
		for (int j=0; j<BIT; ++j)
			pref[i][j]+=pref[i-1][j];
	for (int i=0; i<BIT; ++i)
		for (int j=0; j<BIT; ++j) {
			if (i==j)
				continue;
			for (int k : oc[i])
				first[i][j]+=pref[n][j]-pref[k][j]; // amount of ppl in group j that come after current person
			change[i][j].resize(oc[i].size());
			change_sum[i][j]={0};
			for (int k=0; k<oc[i].size(); ++k) {
				change[i][j][k]=pref[oc[i][k]][j]-(pref[n][j]-pref[oc[i][k]][j]);
				change_sum[i][j].push_back(change_sum[i][j].back()+change[i][j][k]);
			}
		}
	memset(dp, 0x3f, sizeof(dp));
	dp[0]=0;
	for (int mask=0; mask<(1<<BIT); ++mask) {
		for (int i=0; i<BIT; ++i) {
			if (mask&1<<i)
				continue;
			if (oc[i].empty()) {
				dp[mask|1<<i]=min(dp[mask|1<<i], dp[mask]);
				continue;
			}
			int sz=oc[i].size();
			int lb=0, rb=sz;
			while(lb<rb) {
				int mid=(lb+rb+1)/2; // find maximum beneficial change
				int tot_change=mid-1-(sz-mid);
				for (int j=0; j<BIT; ++j)
					if (mask&1<<j)
						tot_change+=2*change[i][j][mid-1];
				if (tot_change<0)
					lb=mid;
				else
					rb=mid-1;
			}
			ll cur=dp[mask]+C2(lb)+C2(sz-lb);
			//if (mask==2)
			//	cout << dp[mask] << " " << lb << " " << cur << " " << first[0][1] << " " << change_sum[0][1][2] << endl;
			for (int j=0; j<BIT; ++j)
				if (mask&1<<j) {
					cur+=2*first[i][j];
					cur+=2*change_sum[i][j][lb];
				}
			dp[mask|1<<i]=min(dp[mask|1<<i], cur);
		}
	}
	ll ans=dp[(1<<BIT)-1];
	cout << ans/2;
	if (ans%2)
		cout << ".5";
	return 0;
}

Compilation message

passes.cpp: In function 'int main()':
passes.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |    for (int k=0; k<oc[i].size(); ++k) {
      |                  ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 828 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 8 ms 832 KB Output is correct
6 Correct 32 ms 19384 KB Output is correct
7 Correct 35 ms 22656 KB Output is correct
8 Correct 38 ms 24068 KB Output is correct
9 Correct 37 ms 24004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 604 KB Output is correct
2 Correct 6 ms 596 KB Output is correct
3 Correct 23 ms 636 KB Output is correct
4 Correct 19 ms 628 KB Output is correct
5 Correct 14 ms 596 KB Output is correct
6 Correct 12 ms 596 KB Output is correct
7 Correct 21 ms 620 KB Output is correct
8 Correct 16 ms 596 KB Output is correct
9 Correct 19 ms 596 KB Output is correct
10 Correct 17 ms 596 KB Output is correct
11 Correct 17 ms 612 KB Output is correct
12 Correct 17 ms 600 KB Output is correct
13 Correct 16 ms 596 KB Output is correct
14 Correct 16 ms 604 KB Output is correct
15 Correct 16 ms 612 KB Output is correct
16 Correct 19 ms 596 KB Output is correct
17 Correct 19 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 604 KB Output is correct
2 Correct 6 ms 596 KB Output is correct
3 Correct 23 ms 636 KB Output is correct
4 Correct 19 ms 628 KB Output is correct
5 Correct 14 ms 596 KB Output is correct
6 Correct 12 ms 596 KB Output is correct
7 Correct 21 ms 620 KB Output is correct
8 Correct 16 ms 596 KB Output is correct
9 Correct 19 ms 596 KB Output is correct
10 Correct 17 ms 596 KB Output is correct
11 Correct 17 ms 612 KB Output is correct
12 Correct 17 ms 600 KB Output is correct
13 Correct 16 ms 596 KB Output is correct
14 Correct 16 ms 604 KB Output is correct
15 Correct 16 ms 612 KB Output is correct
16 Correct 19 ms 596 KB Output is correct
17 Correct 19 ms 608 KB Output is correct
18 Correct 7 ms 596 KB Output is correct
19 Correct 7 ms 628 KB Output is correct
20 Correct 20 ms 636 KB Output is correct
21 Correct 19 ms 596 KB Output is correct
22 Correct 14 ms 632 KB Output is correct
23 Correct 14 ms 596 KB Output is correct
24 Correct 18 ms 624 KB Output is correct
25 Correct 19 ms 608 KB Output is correct
26 Correct 16 ms 600 KB Output is correct
27 Correct 17 ms 612 KB Output is correct
28 Correct 16 ms 612 KB Output is correct
29 Correct 16 ms 596 KB Output is correct
30 Correct 17 ms 612 KB Output is correct
31 Correct 18 ms 604 KB Output is correct
32 Correct 16 ms 604 KB Output is correct
33 Correct 17 ms 616 KB Output is correct
34 Correct 17 ms 604 KB Output is correct
35 Correct 11 ms 3284 KB Output is correct
36 Correct 11 ms 3284 KB Output is correct
37 Correct 42 ms 3156 KB Output is correct
38 Correct 41 ms 2900 KB Output is correct
39 Correct 23 ms 3284 KB Output is correct
40 Correct 40 ms 2900 KB Output is correct
41 Correct 50 ms 3488 KB Output is correct
42 Correct 44 ms 3412 KB Output is correct
43 Correct 40 ms 3284 KB Output is correct
44 Correct 42 ms 3412 KB Output is correct
45 Correct 39 ms 3412 KB Output is correct
46 Correct 40 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 828 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 8 ms 832 KB Output is correct
6 Correct 32 ms 19384 KB Output is correct
7 Correct 35 ms 22656 KB Output is correct
8 Correct 38 ms 24068 KB Output is correct
9 Correct 37 ms 24004 KB Output is correct
10 Correct 9 ms 604 KB Output is correct
11 Correct 6 ms 596 KB Output is correct
12 Correct 23 ms 636 KB Output is correct
13 Correct 19 ms 628 KB Output is correct
14 Correct 14 ms 596 KB Output is correct
15 Correct 12 ms 596 KB Output is correct
16 Correct 21 ms 620 KB Output is correct
17 Correct 16 ms 596 KB Output is correct
18 Correct 19 ms 596 KB Output is correct
19 Correct 17 ms 596 KB Output is correct
20 Correct 17 ms 612 KB Output is correct
21 Correct 17 ms 600 KB Output is correct
22 Correct 16 ms 596 KB Output is correct
23 Correct 16 ms 604 KB Output is correct
24 Correct 16 ms 612 KB Output is correct
25 Correct 19 ms 596 KB Output is correct
26 Correct 19 ms 608 KB Output is correct
27 Correct 7 ms 596 KB Output is correct
28 Correct 7 ms 628 KB Output is correct
29 Correct 20 ms 636 KB Output is correct
30 Correct 19 ms 596 KB Output is correct
31 Correct 14 ms 632 KB Output is correct
32 Correct 14 ms 596 KB Output is correct
33 Correct 18 ms 624 KB Output is correct
34 Correct 19 ms 608 KB Output is correct
35 Correct 16 ms 600 KB Output is correct
36 Correct 17 ms 612 KB Output is correct
37 Correct 16 ms 612 KB Output is correct
38 Correct 16 ms 596 KB Output is correct
39 Correct 17 ms 612 KB Output is correct
40 Correct 18 ms 604 KB Output is correct
41 Correct 16 ms 604 KB Output is correct
42 Correct 17 ms 616 KB Output is correct
43 Correct 17 ms 604 KB Output is correct
44 Correct 11 ms 3284 KB Output is correct
45 Correct 11 ms 3284 KB Output is correct
46 Correct 42 ms 3156 KB Output is correct
47 Correct 41 ms 2900 KB Output is correct
48 Correct 23 ms 3284 KB Output is correct
49 Correct 40 ms 2900 KB Output is correct
50 Correct 50 ms 3488 KB Output is correct
51 Correct 44 ms 3412 KB Output is correct
52 Correct 40 ms 3284 KB Output is correct
53 Correct 42 ms 3412 KB Output is correct
54 Correct 39 ms 3412 KB Output is correct
55 Correct 40 ms 3284 KB Output is correct
56 Correct 12 ms 612 KB Output is correct
57 Correct 26 ms 596 KB Output is correct
58 Correct 8 ms 832 KB Output is correct
59 Correct 4 ms 596 KB Output is correct
60 Correct 6 ms 600 KB Output is correct
61 Correct 5 ms 596 KB Output is correct
62 Correct 9 ms 812 KB Output is correct
63 Correct 32 ms 19268 KB Output is correct
64 Correct 36 ms 22676 KB Output is correct
65 Correct 37 ms 24012 KB Output is correct
66 Correct 47 ms 24048 KB Output is correct
67 Correct 7 ms 596 KB Output is correct
68 Correct 7 ms 596 KB Output is correct
69 Correct 20 ms 596 KB Output is correct
70 Correct 20 ms 632 KB Output is correct
71 Correct 14 ms 640 KB Output is correct
72 Correct 11 ms 596 KB Output is correct
73 Correct 18 ms 604 KB Output is correct
74 Correct 14 ms 724 KB Output is correct
75 Correct 16 ms 596 KB Output is correct
76 Correct 17 ms 604 KB Output is correct
77 Correct 17 ms 596 KB Output is correct
78 Correct 19 ms 612 KB Output is correct
79 Correct 17 ms 616 KB Output is correct
80 Correct 17 ms 596 KB Output is correct
81 Correct 17 ms 732 KB Output is correct
82 Correct 17 ms 596 KB Output is correct
83 Correct 16 ms 596 KB Output is correct
84 Correct 10 ms 3284 KB Output is correct
85 Correct 11 ms 3284 KB Output is correct
86 Correct 41 ms 3172 KB Output is correct
87 Correct 41 ms 3004 KB Output is correct
88 Correct 20 ms 3304 KB Output is correct
89 Correct 38 ms 2900 KB Output is correct
90 Correct 43 ms 3412 KB Output is correct
91 Correct 47 ms 3396 KB Output is correct
92 Correct 41 ms 3284 KB Output is correct
93 Correct 46 ms 3412 KB Output is correct
94 Correct 39 ms 3412 KB Output is correct
95 Correct 41 ms 3304 KB Output is correct
96 Correct 90 ms 25804 KB Output is correct
97 Correct 24 ms 596 KB Output is correct
98 Correct 95 ms 25600 KB Output is correct
99 Correct 61 ms 24012 KB Output is correct
100 Correct 20 ms 620 KB Output is correct
101 Correct 82 ms 25620 KB Output is correct
102 Correct 88 ms 25804 KB Output is correct
103 Correct 105 ms 25456 KB Output is correct
104 Correct 92 ms 25468 KB Output is correct
105 Correct 87 ms 25608 KB Output is correct
106 Correct 89 ms 25744 KB Output is correct
107 Correct 94 ms 26184 KB Output is correct
108 Correct 90 ms 25196 KB Output is correct
109 Correct 100 ms 26600 KB Output is correct