Submission #723396

#TimeUsernameProblemLanguageResultExecution timeMemory
723396drdilyorBoarding Passes (BOI22_passes)C++17
60 / 100
2099 ms377004 KiB
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using ll = long long; #define time(code) {auto _s = chrono::high_resolution_clock::now(); code;cerr << (chrono::high_resolution_clock::now() - _s).count() / 1e9 << '\n';} inline ll exp_inv(ll x) { return x * (x-1); } int main() { cin.tie(0)->sync_with_stdio(0); char a = 'A'; string s; cin >> s; const int G = 15, N = 1e5; int n = s.size(); if (n == 1) { cout << "0\n"; return 0; } ll suml[G][G][N]; ll sumr[G][G][N]; ll cr[G][N]; ll cl[G][N]; int cnt[G]{}; vector ix(G, vector<int>()); time(({ for (int c1 = 0; c1 < G; c1++) { for (int c2 = 0; c2 < G; c2++) { ll res = 0, cur = 0; for (int i = 0; i < n; i++) { if (s[i] == c1+a) res += cur; else if (s[i] == c2+a) cur++; suml[c1][c2][i] = res; } } } for (int c1 = 0; c1 < G; c1++) { for (int c2 = 0; c2 < G; c2++) { ll res = 0, cur = 0; for (int i = n-1; i >= 0; i --) { if (s[i] == c1+a) res += cur; else if (s[i] == c2+a) cur++; sumr[c1][c2][i] = res; } } } for (int j = 0; j < G; j++) { int c = 0; for (int i = 0; i < n;i ++) { if (s[i] == j+a) c++; cl[j][i] = c; } } for (int j = 0; j < G; j++) { int c = 0; for (int i = n-1; i >= 0; i--) { if (s[i] == j+a) c++; cr[j][i] = c; } } for (int i = 0; i < n; i++) cnt[s[i] - a]++; for (int i = 0; i < G; i++) ix[i].reserve(cnt[i]); for (int i = 0; i < n ;i++) ix[s[i] - a].push_back(i); })); ll memo[1<<G]; memo[(1<<G)-1] = 0; for (int used = (1<<G)-1-1; used >= 0; used--) { ll& res = memo[used]; res = 1e18; for (int i = 0 ;i < G; i++) { if (used&1<<i) continue; auto f = [&](int i, int j) { ll sum = 0; if (j) for (int c2 = 0; c2 < G; c2++) sum += (used>>c2&1) * suml[i][c2][j-1]; if (j < n) for (int c2 = 0; c2 < G; c2++) sum += (used>>c2&1) * sumr[i][c2][j]; return sum * 4 + (j < n ? exp_inv(cr[i][j]) : 0) + (j ? exp_inv(cl[i][j-1]) : 0); }; int l = -1, r = (int)ix[i].size()-2; while (l < r-1) { int m = (l+r) / 2; ll a = f(i, ix[i][m]), b = f(i, ix[i][m+1]); if (a == b) break; else if (a < b) r = m; else l = m; } ll cur = min(f(i, 0), f(i, n));; for (int j = max(l-1, 0); j < min((int)ix[i].size(), 2 + max(0, r)); j++) { cur = min(cur, f(i, ix[i][j])); } res = min(res, cur + memo[used|1<<i]); } }; ll ans; ans = memo[0]; cout << ans / 4; if (ans%4 == 1) cout << ".25"; if (ans%4 == 2) cout << ".5"; if (ans%4== 3) cout << ".75"; cout << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...