This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}
size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#endif
typedef long long ll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr ll INF = 1e18;
void solve() {
//ifstream cin("nazwa.in");
//ofstream cout("nazwa.out");
auto wypisz = [&](string a) {
for(auto x : a) {
cout << x;
}
cout << '\n';
};
string c;
cin >> c;
for(auto &x : c) {
x += 32;
}
vi con(26, 0);
for(auto x : c) {
con[x - 'a'] = 1;
}
int nr = 0;
for(int i = 0; i < 26; i++) {
if(con[i]) {
con[i] = nr++;
}
}
int mx = 0;
for(auto &x : c) {
x = 'a' + con[x - 'a'];
mx = max(mx, x - 'a');
}
mx++;
vector<vi>wys(mx);
for(int i = 0; i < sz(c); i++) {
wys[c[i] - 'a'].eb(i);
}
vector<vector<vll> >pref(mx, vector<vll>(mx));
for(int i = 0; i < mx; i++) {
for(int j = 0; j < mx; j++) {
int kt = 0;
for(int k = 0; k < sz(wys[j]); k++) {
while(kt < sz(wys[i]) && wys[i][kt] < wys[j][k]) {
kt++;
}
pref[i][j].eb(kt);
if(k > 0) {
pref[i][j][k] += pref[i][j][k - 1];
}
}
}
}
vector<vector<vll> >suf(mx, vector<vll>(mx));
for(int i = 0; i < mx; i++) {
for(int j = 0; j < mx; j++) {
int kt = sz(wys[i]);
for(int k = sz(wys[j]) - 1; k >= 0; k--) {
while(kt > 0 && wys[i][kt - 1] > wys[j][k]) {
kt--;
}
suf[i][j].eb(sz(wys[i]) - kt);
if(sz(suf[i][j]) > 1) {
suf[i][j].back() += suf[i][j][sz(suf[i][j]) - 2];
}
}
reverse(all(suf[i][j]));
}
}
vector<ld> dp((1 << mx), INF);
dp[0] = 0;
auto cost = [&](int mask, int kt, int x) {
ld ans = 0;
if(x > 0) {
ans = (ans + (ld)x * (x - 1) / (ld)4);
for(int i = 0; i < mx; i++) {
if(mask & (1 << i)) {
//debug(mask, i, kt, sz(pref[i][kt]), x);
ans = (ans + pref[i][kt][x - 1]);
}
}
}
if(x < sz(wys[kt])) {
ans = (ans + (ld)(sz(wys[kt]) - x) * (sz(wys[kt]) - x - 1) / (ld)4);
for(int i = 0; i < mx; i++) {
if(mask & (1 << i)) {
ans = (ans + suf[i][kt][x]);
}
}
}
return ans;
};
//wypisz(c);
auto get = [&](int mask, int bit) {
for(int i = 0; i <= sz(wys[bit]); i++) {
debug(i, cost(mask, bit, i));
}
int l = 0, r = sz(wys[bit]);
while(l < r) {
int mid1 = l + ((r - l) / 3);
int mid2 = r - ((r - l) / 3);
debug(l, r, mid1, mid2);
ld a = cost(mask, bit, mid1);
ld b = cost(mask, bit, mid2);
//debug(l, r, mid1, mid2, a, b);
if(a < b) {
r = mid2 - 1;
}
else if(a == b) {
l = mid1, r = mid2 - 1;
}
else {
l = mid1 + 1;
}
}
return cost(mask, bit, l);
};
//cout << fixed << get(2, 0) << '\n';
for(int mask = 0; mask < (1 << mx); mask++) {
for(int i = 0; i < mx; i++) {
if(!(mask & (1 << i))) {
dp[mask ^ (1 << i)] = min(dp[mask ^ (1 << i)], dp[mask] + get(mask, i));
}
}
debug(mask, dp[mask]);
}
cout.precision(1);
cout << fixed << dp[(1 << mx) - 1] << '\n';
}
int main() {
fastio();
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
Compilation message (stderr)
passes.cpp: In function 'void solve()':
passes.cpp:41:7: warning: variable 'wypisz' set but not used [-Wunused-but-set-variable]
41 | auto wypisz = [&](string a) {
| ^~~~~~
# | 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... |