Submission #652281

# Submission time Handle Problem Language Result Execution time Memory
652281 2022-10-22T02:03:48 Z inksamurai Boarding Passes (BOI22_passes) C++17
5 / 100
7 ms 4412 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3CZAtRo ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

const int inf=1e10;

void chmin(int&a,const int&b){
	a=min(a,b);
}

signed main(){
_3CZAtRo;
	int n;
	vi a;
	{
		string s;
		cin>>s;
		n=sz(s);
		vi tmp;
		rep(i,n){
			tmp.pb(s[i]-'A');
		}
		sort(tmp.begin(),tmp.end());
		tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
		rep(i,n){
			int v=lower_bound(tmp.begin(),tmp.end(),s[i]-'A')-tmp.begin();
			a.pb(v);
		}
	}

	// rep(i,n){
	// 	cout<<a[i]<<" ";
	// }
	// print();

	const int si=sz(set<int>(a.begin(),a.end()));
	vec(vi) rbts(si);
	rep(i,n){
		rbts[a[i]].pb(i);
	}

	vec(vec(vi)) pf(si,vec(vi)(si,vi(n))),sf;
	sf=pf;
	rep(s,si){
		rep(t,si){
			if(s==t) continue;
			int cnt=0;
			rep(i,n){
				if(i){
					pf[s][t][i]=pf[s][t][i-1];
				}
				if(a[i]==t){
					pf[s][t][i]+=cnt;
				}else if(a[i]==s){
					cnt+=1;
				}
			}
			cnt=0;
			per(i,n){
				if(i<n-1){
					sf[s][t][i]=sf[s][t][i+1];
				}
				if(a[i]==t){
					sf[s][t][i]+=cnt;
				}else if(a[i]==s){
					cnt+=1;
				}
			}
		}
	}
	
	auto af1=[&](int v,int msk,int id)->int{
		int pos=(id==-1?id:rbts[v][id]);
		int res=0;
		rep(u,si){
			if(msk>>u&1){
				if(pos!=-1){
					res+=pf[u][v][pos];
				}
				if(pos+1<n){
					res+=sf[u][v][pos+1];
				}
			}
		}
		res=2*res+((id+1)*id)/2+(sz(rbts[v])-id-1)*(sz(rbts[v])-id-2)/2;
		return res;
	};

	auto af0=[&](int v,int msk){
		int sj=sz(rbts[v]);
		int l=0,r=sj-1;
		while(r-l>3){
			int ml=(2*l+r)/3;
			int mr=(l+2*r)/3;
			int fl=af1(v,msk,ml);
			int fr=af1(v,msk,mr);
			if(fl<fr)r=mr;
			else l=ml;
		}
		int res=af1(v,msk,-1);
		rng(i,l,r+1){
			res=min(res,af1(v,msk,rbts[v][i]));
		}
		return res;
	};

	vi dp(1<<si,inf);
	dp[0]=0;
	rep(msk,1<<si){
		int now=dp[msk];
		rep(v,si){
			if(!(msk>>v&1)){
				int nmsk=msk|(1<<v);
				int wit=af0(v,msk);
				// print(msk,v,wit);
				chmin(dp[nmsk],wit+now);
			}
		}
	}

	int ans=dp[(1<<si)-1];
	cout<<ans/2<<(ans%2?".5":"")<<"\n";
}


# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB found '100800.5000000000', expected '100800.5000000000', error '0.0000000000'
2 Correct 0 ms 212 KB found '0.0000000000', expected '0.0000000000', error '-0.0000000000'
3 Correct 0 ms 212 KB found '0.0000000000', expected '0.0000000000', error '-0.0000000000'
4 Correct 0 ms 212 KB found '1.0000000000', expected '1.0000000000', error '0.0000000000'
5 Correct 1 ms 340 KB found '124002.0000000000', expected '124002.0000000000', error '0.0000000000'
6 Correct 5 ms 3792 KB found '772893586.0000000000', expected '772893586.0000000000', error '0.0000000000'
7 Correct 6 ms 4204 KB found '1100977812.5000000000', expected '1100977812.5000000000', error '0.0000000000'
8 Correct 6 ms 4412 KB found '1249950000.5000000000', expected '1249950000.5000000000', error '0.0000000000'
9 Correct 7 ms 4412 KB found '1249975000.0000000000', expected '1249975000.0000000000', error '0.0000000000'
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB 1st numbers differ - expected: '1.0000000000', found: '1.5000000000', error = '0.5000000000'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB 1st numbers differ - expected: '1.0000000000', found: '1.5000000000', error = '0.5000000000'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB found '100800.5000000000', expected '100800.5000000000', error '0.0000000000'
2 Correct 0 ms 212 KB found '0.0000000000', expected '0.0000000000', error '-0.0000000000'
3 Correct 0 ms 212 KB found '0.0000000000', expected '0.0000000000', error '-0.0000000000'
4 Correct 0 ms 212 KB found '1.0000000000', expected '1.0000000000', error '0.0000000000'
5 Correct 1 ms 340 KB found '124002.0000000000', expected '124002.0000000000', error '0.0000000000'
6 Correct 5 ms 3792 KB found '772893586.0000000000', expected '772893586.0000000000', error '0.0000000000'
7 Correct 6 ms 4204 KB found '1100977812.5000000000', expected '1100977812.5000000000', error '0.0000000000'
8 Correct 6 ms 4412 KB found '1249950000.5000000000', expected '1249950000.5000000000', error '0.0000000000'
9 Correct 7 ms 4412 KB found '1249975000.0000000000', expected '1249975000.0000000000', error '0.0000000000'
10 Incorrect 0 ms 212 KB 1st numbers differ - expected: '1.0000000000', found: '1.5000000000', error = '0.5000000000'
11 Halted 0 ms 0 KB -