Submission #978591

# Submission time Handle Problem Language Result Execution time Memory
978591 2024-05-09T11:12:15 Z jmyszka2007 Boarding Passes (BOI22_passes) C++17
25 / 100
1 ms 856 KB
#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) / 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) / 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 << dp[(1 << mx) - 1] << '\n';
}
int main() {
	fastio();
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}

Compilation message

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
1 Incorrect 0 ms 344 KB 1st numbers differ - expected: '100800.5000000000', found: '100800.0000000000', error = '0.0000049603'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB found '1.0000000000', expected '1.0000000000', error '0.0000000000'
2 Correct 0 ms 348 KB found '1225.0000000000', expected '1225.0000000000', error '0.0000000000'
3 Correct 1 ms 348 KB found '1023.0000000000', expected '1023.0000000000', error '0.0000000000'
4 Correct 0 ms 348 KB found '294.0000000000', expected '294.0000000000', error '0.0000000000'
5 Correct 1 ms 348 KB found '1087.0000000000', expected '1087.0000000000', error '0.0000000000'
6 Correct 0 ms 348 KB found '1.5000000000', expected '1.5000000000', error '0.0000000000'
7 Correct 1 ms 348 KB found '703.0000000000', expected '703.0000000000', error '0.0000000000'
8 Correct 1 ms 348 KB found '55.5000000000', expected '55.5000000000', error '0.0000000000'
9 Correct 1 ms 348 KB found '56.0000000000', expected '56.0000000000', error '0.0000000000'
10 Correct 1 ms 348 KB found '45.0000000000', expected '45.0000000000', error '0.0000000000'
11 Correct 1 ms 348 KB found '66.5000000000', expected '66.5000000000', error '0.0000000000'
12 Correct 1 ms 348 KB found '67.0000000000', expected '67.0000000000', error '0.0000000000'
13 Correct 1 ms 456 KB found '66.0000000000', expected '66.0000000000', error '0.0000000000'
14 Correct 1 ms 348 KB found '47.0000000000', expected '47.0000000000', error '0.0000000000'
15 Correct 1 ms 348 KB found '50.0000000000', expected '50.0000000000', error '0.0000000000'
16 Correct 1 ms 348 KB found '49.0000000000', expected '49.0000000000', error '0.0000000000'
17 Correct 1 ms 348 KB found '57.0000000000', expected '57.0000000000', error '0.0000000000'
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB found '1.0000000000', expected '1.0000000000', error '0.0000000000'
2 Correct 0 ms 348 KB found '1225.0000000000', expected '1225.0000000000', error '0.0000000000'
3 Correct 1 ms 348 KB found '1023.0000000000', expected '1023.0000000000', error '0.0000000000'
4 Correct 0 ms 348 KB found '294.0000000000', expected '294.0000000000', error '0.0000000000'
5 Correct 1 ms 348 KB found '1087.0000000000', expected '1087.0000000000', error '0.0000000000'
6 Correct 0 ms 348 KB found '1.5000000000', expected '1.5000000000', error '0.0000000000'
7 Correct 1 ms 348 KB found '703.0000000000', expected '703.0000000000', error '0.0000000000'
8 Correct 1 ms 348 KB found '55.5000000000', expected '55.5000000000', error '0.0000000000'
9 Correct 1 ms 348 KB found '56.0000000000', expected '56.0000000000', error '0.0000000000'
10 Correct 1 ms 348 KB found '45.0000000000', expected '45.0000000000', error '0.0000000000'
11 Correct 1 ms 348 KB found '66.5000000000', expected '66.5000000000', error '0.0000000000'
12 Correct 1 ms 348 KB found '67.0000000000', expected '67.0000000000', error '0.0000000000'
13 Correct 1 ms 456 KB found '66.0000000000', expected '66.0000000000', error '0.0000000000'
14 Correct 1 ms 348 KB found '47.0000000000', expected '47.0000000000', error '0.0000000000'
15 Correct 1 ms 348 KB found '50.0000000000', expected '50.0000000000', error '0.0000000000'
16 Correct 1 ms 348 KB found '49.0000000000', expected '49.0000000000', error '0.0000000000'
17 Correct 1 ms 348 KB found '57.0000000000', expected '57.0000000000', error '0.0000000000'
18 Correct 0 ms 348 KB found '1.0000000000', expected '1.0000000000', error '0.0000000000'
19 Correct 0 ms 348 KB found '1225.0000000000', expected '1225.0000000000', error '0.0000000000'
20 Correct 1 ms 348 KB found '1023.0000000000', expected '1023.0000000000', error '0.0000000000'
21 Correct 1 ms 412 KB found '294.0000000000', expected '294.0000000000', error '0.0000000000'
22 Correct 1 ms 348 KB found '1087.0000000000', expected '1087.0000000000', error '0.0000000000'
23 Correct 0 ms 348 KB found '1.5000000000', expected '1.5000000000', error '0.0000000000'
24 Correct 1 ms 348 KB found '703.0000000000', expected '703.0000000000', error '0.0000000000'
25 Correct 0 ms 348 KB found '55.5000000000', expected '55.5000000000', error '0.0000000000'
26 Correct 1 ms 348 KB found '56.0000000000', expected '56.0000000000', error '0.0000000000'
27 Correct 0 ms 452 KB found '45.0000000000', expected '45.0000000000', error '0.0000000000'
28 Correct 1 ms 348 KB found '66.5000000000', expected '66.5000000000', error '0.0000000000'
29 Correct 1 ms 348 KB found '67.0000000000', expected '67.0000000000', error '0.0000000000'
30 Correct 0 ms 344 KB found '66.0000000000', expected '66.0000000000', error '0.0000000000'
31 Correct 1 ms 348 KB found '47.0000000000', expected '47.0000000000', error '0.0000000000'
32 Correct 0 ms 348 KB found '50.0000000000', expected '50.0000000000', error '0.0000000000'
33 Correct 0 ms 452 KB found '49.0000000000', expected '49.0000000000', error '0.0000000000'
34 Correct 1 ms 348 KB found '57.0000000000', expected '57.0000000000', error '0.0000000000'
35 Correct 1 ms 856 KB found '12497500.0000000000', expected '12497500.0000000000', error '0.0000000000'
36 Incorrect 1 ms 600 KB 1st numbers differ - expected: '12495000.5000000000', found: '12495000.0000000000', error = '0.0000000400'
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB 1st numbers differ - expected: '100800.5000000000', found: '100800.0000000000', error = '0.0000049603'
2 Halted 0 ms 0 KB -