# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
850415 | 2023-09-16T14:18:27 Z | Ahmed57 | Boarding Passes (BOI22_passes) | C++17 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; int c = 0; vector<int> arr[15]; short pref[1024][10001],suf[1024][10001]; long double dp[1024]; int sz; long double ge(long double x){ if(x<=1)return 0; return (x*(x-1))/4.0; } int main(){ string s;cin>>s; map<char,int> x; int ind = -1; sz = s.size(); for(auto i:s){ ind++; if(x[i]==0){ x[i] = ++c; arr[c-1].push_back(ind); }else{ arr[x[i]-1].push_back(ind); } } if(c>10){ cout<<"a3"<<endl; return 0; } for(int mask = 0;mask<(1<<c);mask++){ for(int i = 0;i<sz;i++){ pref[i][mask] = (mask&(1<<(x[s[i]]-1))?1:0); if(i)pref[i][mask]+=pref[i-1][mask]; } for(int i = sz-1;i>=0;i--){ suf[i][mask] =(mask&(1<<(x[s[i]]-1))?1:0); if(i<sz-1)suf[i][mask]+=suf[i+1][mask]; } } for(int mask = (1<<c)-1;mask>=0;mask--){ if(mask==(1<<c)-1){ dp[mask] = 0; continue; } dp[mask] = 1000000000.0; for(int j = 0;j<c;j++){ if(!(mask&(1<<j))){ long long all = 0 , xd = 0; for(int e = 0;e<arr[j].size();e++){ all+=suf[arr[j][e]][mask]; } dp[mask] = min(dp[mask],solve(mask^(1<<j))+all+xd+ge(arr[j].size())); for(int e = 0;e<arr[j].size();e++){ all-=suf[arr[j][e]][mask]; xd+=pref[arr[j][e]][mask]; dp[mask] = min(dp[mask],dp[mask^(1<<j)]+all+xd+ge(e+1)+ge(arr[j].size()-e-1)); } } } } cout<<setprecision(4)<<fixed<<dp[0]<<endl; return 0; }