Submission #628651

# Submission time Handle Problem Language Result Execution time Memory
628651 2022-08-13T14:49:11 Z vovamr Ekoeko (COCI21_ekoeko) C++17
110 / 110
17 ms 5984 KB
#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 time Memory Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 5 ms 2832 KB Output is correct
3 Correct 11 ms 4452 KB Output is correct
4 Correct 14 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 5 ms 2832 KB Output is correct
3 Correct 11 ms 4452 KB Output is correct
4 Correct 14 ms 5980 KB Output is correct
5 Correct 1 ms 1220 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 5 ms 2832 KB Output is correct
3 Correct 11 ms 4452 KB Output is correct
4 Correct 14 ms 5980 KB Output is correct
5 Correct 1 ms 1244 KB Output is correct
6 Correct 14 ms 5984 KB Output is correct
7 Correct 15 ms 5984 KB Output is correct
8 Correct 15 ms 5856 KB Output is correct
9 Correct 17 ms 5808 KB Output is correct
10 Correct 14 ms 5856 KB Output is correct
11 Correct 14 ms 5744 KB Output is correct
12 Correct 15 ms 5792 KB Output is correct
13 Correct 13 ms 5624 KB Output is correct
14 Correct 13 ms 5600 KB Output is correct
15 Correct 14 ms 5616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 5 ms 2832 KB Output is correct
3 Correct 11 ms 4452 KB Output is correct
4 Correct 14 ms 5980 KB Output is correct
5 Correct 1 ms 1220 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1244 KB Output is correct
10 Correct 14 ms 5984 KB Output is correct
11 Correct 15 ms 5984 KB Output is correct
12 Correct 15 ms 5856 KB Output is correct
13 Correct 17 ms 5808 KB Output is correct
14 Correct 14 ms 5856 KB Output is correct
15 Correct 14 ms 5744 KB Output is correct
16 Correct 15 ms 5792 KB Output is correct
17 Correct 13 ms 5624 KB Output is correct
18 Correct 13 ms 5600 KB Output is correct
19 Correct 14 ms 5616 KB Output is correct
20 Correct 1 ms 1236 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1108 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1108 KB Output is correct
30 Correct 1 ms 1108 KB Output is correct
31 Correct 1 ms 1208 KB Output is correct
32 Correct 15 ms 5900 KB Output is correct
33 Correct 16 ms 5864 KB Output is correct
34 Correct 14 ms 5852 KB Output is correct
35 Correct 16 ms 5864 KB Output is correct
36 Correct 13 ms 5728 KB Output is correct
37 Correct 14 ms 5716 KB Output is correct
38 Correct 13 ms 5728 KB Output is correct
39 Correct 15 ms 5720 KB Output is correct
40 Correct 14 ms 5600 KB Output is correct
41 Correct 13 ms 5600 KB Output is correct