답안 #582111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582111 2022-06-23T11:46:56 Z amunduzbaev Boarding Passes (BOI22_passes) C++17
5 / 100
3 ms 3156 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;
#define int ll

const int G = 15;
const int N = 1e5 + 5;
int dp[1 << G]; 
int pref[G][G][N], cc[G][N];
int suff[G][G][N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	string s; cin>>s;
	int n = s.size(), g = 0;
	for(int i=0;i<n;i++){
		g = max(g, (ll)s[i] - 'A' + 1);
	}
	for(int i=0;i<n;i++){
		if(i){
			for(int j=0;j<g;j++) cc[j][i] = cc[j][i-1];
		}
		
		cc[s[i] - 'A'][i]++;
	}
	
	for(int a=0;a<g;a++){
		for(int b=0;b<g;b++){
			for(int i=0;i<n;i++){
				if(i) pref[a][b][i] = pref[a][b][i - 1];
				if(s[i] - 'A' == a){
					pref[a][b][i] += cc[b][i] * 2;
				}
			}
			for(int i=n-1;~i;i--){
				suff[a][b][i] = suff[a][b][i + 1];
				if(s[i] - 'A' == a){
					suff[a][b][i] += (cc[b][n - 1] - cc[b][i]) * 2;
				}
			}
		}
	}
	
	memset(dp, 63, sizeof dp);
	dp[0] = 0;
	for(int mask=0;mask < (1 << g);mask++){
		//~ for(int k=0;k<g;k++) cout<<(mask >> k & 1);
		//~ cout<<" : "<<dp[mask]<<"\n";
		for(int j=0;j<g;j++){
			if(mask >> j & 1) continue;
			auto check = [&](int i){
				int res = 0;
				for(int k=0;k<g;k++){
					if(mask >> k & 1){
						if(i) res += pref[j][k][i - 1];
						res += suff[j][k][i];
					}
				}
				if(i){
					int m = cc[j][i - 1];
					if(m) res = res + m * (m - 1) / 2;
				}
				
				int m = cc[j][n - 1] - (i ? cc[j][i - 1] : 0);
				if(m) res = res + m * (m - 1) / 2;
				return res;
			};
			
			int l = 0, r = n;
			while(l + 1 < r - 1){
				int m = (l + r) >> 1;
				if(check(m) <= check(m + 1)) r = m + 1;
				else l = m;
			}
			
			int res = min({check(l), check(r), check(l + 1)});

			//~ for(int l=0;l<=n;l++) cout<<check(l)<<" ";
			//~ cout<<"\n";
			//~ cout<<res<<"\n";
			
			dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + res);
		}
	}
	
	int res = dp[(1 << g) - 1];
	cout<<res/2;
	if(res&1) cout<<".5";
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 2632 KB Output is correct
7 Correct 3 ms 2968 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Incorrect 1 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Incorrect 1 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 2632 KB Output is correct
7 Correct 3 ms 2968 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Incorrect 1 ms 1236 KB Output isn't correct
13 Halted 0 ms 0 KB -