Submission #628651

#TimeUsernameProblemLanguageResultExecution timeMemory
628651vovamrEkoeko (COCI21_ekoeko)C++17
110 / 110
17 ms5984 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fi first #define se second #define ll long long #define ld long double #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define pb push_back #define mpp make_pair #define ve vector using namespace std; using namespace __gnu_pbds; template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const ll inf = 1e18; const int iinf = 1e9; typedef pair<ll, ll> pll; typedef pair<int, int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); } template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); } const int N = 2e5 + 10; int f[N]; inline void upd(int i, int x) { i += 2; for (; i < N; i += i & -i) f[i] += x; } inline int get(int i) { i += 2; int ans = 0; for (; i; i -= i & -i){ans+=f[i];} return ans; } inline void solve() { int n; string s; cin >> n >> s; n *= 2; ve<int> right(n), left(n); ve<ve<int>> occ(26); for (int i = 0; i < n; ++i) occ[s[i] - 'a'].pb(i); for (int c = 0; c < 26; ++c) { int s = sz(occ[c]); for (int i = s / 2; i < s; ++i) right[occ[c][i]] = 1; for (int i = 0; i < s / 2; ++i) left[occ[c][i]] = 1; } ve<int> pos(n); int usl = 0, usr = 0; for (int i = 0; i < n; ++i) { if (left[i]) pos[i] = usl++; else pos[i] = n / 2 + usr++; } ll ans = 0; for (int i = n - 1; ~i; --i) { ans += get(pos[i] - 1); upd(pos[i], +1); } string res; res.resize(n); for (int i = 0; i < n; ++i) { res[pos[i]] = s[i]; } int si = sz(res); string a = res.substr(0, si / 2); string b = res.substr(si / 2, si / 2); ve<ve<int>> ps(26); for (int i = sz(a) - 1; ~i; --i) ps[a[i] - 'a'].pb(i); ve<int> tr(sz(b)); for (int i = 0; i < sz(b); ++i) { tr[i] = ps[b[i] - 'a'].back(); ps[b[i] - 'a'].pop_back(); } memset(f, 0, sizeof(f)); for (int i = sz(b) - 1; ~i; --i) { ans += get(tr[i] - 1); upd(tr[i], +1); } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q = 1; // cin >> q; while (q--) solve(); cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << 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...
#Verdict Execution timeMemoryGrader output
Fetching results...