# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
666511 | mychecksedad | Boarding Passes (BOI22_passes) | C++17 | 405 ms | 1856 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
const int N = 1e5 + 100;
int n;
string s;
double dp[N];
vector<int> v[15], pref, vis;
double calc(vector<int> p){
pref.clear(); pref.resize(n);
vis.clear(); vis.resize(n);
double S = 0;
for(int i = 0; i < p.size(); ++i){
long long a = v[p[i]].size();
double best = -1;
long long cur = 0;
int po = n - 1;
for(int j = a - 1; j >= 0; --j){
int pos = v[p[i]][j];
cur += pref[n - 1] - pref[pos];
}
best = cur + a*(a-1)/4.0;
for(double k = 1; k <= a; k += 1){
int pos = v[p[i]][k - 1];
cur -= pref[n - 1] - pref[pos];
cur += pref[pos];
double k1 = a - k;
double val = (k*(k-1) + k1*(k1-1))/4.0 + cur;
best = (best == -1 ? val : min(val, best));
}
for(int pos: v[p[i]]) vis[pos] = 1;
for(int j = 0; j < n; ++j){
if(j == 0) pref[j] = vis[j];
else pref[j] = pref[j - 1] + vis[j];
}
S += best;
}
return S;
}
void solve(){
cin >> s; n = s.length();
for(int i = 0; i < n; ++i) v[s[i]-'A'].push_back(i);
vector<int> p;
double ans = 0;
for(int i = 0; i < 15; ++i){
double best = -1;
int pos = 0;
for(int j = 0; j <= i; ++j){
p.insert(p.begin() + j, i);
double ans = calc(p);
if(best == -1 || best > ans) best = ans, pos = j;
p.erase(p.begin() + j);
}
ans = best;
p.insert(p.begin() + pos, i);
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
setp();
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |