#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(v) v.begin(), v.end()
#define ll long long
#define ull unsigned long long
#define lb long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define cd complex<double>
#define int ll
const double PI = acos(-1);
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pii, null_type,less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// - insert(x),erase(x)
// - find_by_order(k): return iterator to the k-th smallest element
// - order_of_key(x): the number of elements that are strictly smaller
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
struct fenwicktree {
int n;
vector<int> a;
fenwicktree(int _n) {
n = _n;
a.assign(n + 1, 0);
}
void update(int i, int val) {
for(; i; i -= i & -i) a[i] += val;
}
int get(int i) {
int res(0);
for(; i <= n; i += i & -i) res += a[i];
return res;
}
};
vector<int> pos[26];
void process() {
int n; string s;
cin >> n >> s; s = ' ' + s;
vector<int> cnt(26, 0), cur(26, 0);
vector<bool> Left(2 * n + 5, 0);
FOR(i, 1, 2 * n) cnt[s[i] - 'a']++;
FOR(i, 1, 2 * n) {
int c = s[i] - 'a';
cur[c]++;
Left[i] = (cur[c] <= cnt[c] / 2);
}
ll ans = 0;
int res(0);
string nw = " ";
FOR(i, 1, 2 * n) if(Left[i]) {
nw += s[i];
++res;
ans += i - res;
}
FOR(i, 1, 2 * n) if(!Left[i]) nw += s[i];
vector<int> a;
FORD(i, n, 1) pos[nw[i] - 'a'].push_back(i);
FOR(i, n + 1, 2 * n) {
a.push_back(pos[nw[i] - 'a'].back());
pos[nw[i] - 'a'].pop_back();
}
fenwicktree bit(n);
for(int &x : a) {
ans += bit.get(x);
bit.update(x, 1);
}
cout << ans << '\n';
}
signed main() {
if(fopen("eko.inp","r")) {
freopen("eko.inp","r",stdin);
freopen("eko.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// clock_t start = clock()
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
return 0;
}
// https://oj.uz/problem/submit/COCI21_ekoeko
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen("eko.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen("eko.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
4 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
3868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
4 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
3868 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
4 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
3868 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
3852 KB |
Output is correct |
7 |
Correct |
7 ms |
3612 KB |
Output is correct |
8 |
Correct |
8 ms |
3572 KB |
Output is correct |
9 |
Correct |
6 ms |
3616 KB |
Output is correct |
10 |
Correct |
6 ms |
3576 KB |
Output is correct |
11 |
Correct |
6 ms |
3656 KB |
Output is correct |
12 |
Correct |
6 ms |
3620 KB |
Output is correct |
13 |
Correct |
6 ms |
3624 KB |
Output is correct |
14 |
Correct |
6 ms |
3508 KB |
Output is correct |
15 |
Correct |
7 ms |
3376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
4 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
3868 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
3852 KB |
Output is correct |
11 |
Correct |
7 ms |
3612 KB |
Output is correct |
12 |
Correct |
8 ms |
3572 KB |
Output is correct |
13 |
Correct |
6 ms |
3616 KB |
Output is correct |
14 |
Correct |
6 ms |
3576 KB |
Output is correct |
15 |
Correct |
6 ms |
3656 KB |
Output is correct |
16 |
Correct |
6 ms |
3620 KB |
Output is correct |
17 |
Correct |
6 ms |
3624 KB |
Output is correct |
18 |
Correct |
6 ms |
3508 KB |
Output is correct |
19 |
Correct |
7 ms |
3376 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
7 ms |
3840 KB |
Output is correct |
33 |
Correct |
6 ms |
3848 KB |
Output is correct |
34 |
Correct |
6 ms |
3616 KB |
Output is correct |
35 |
Correct |
6 ms |
3792 KB |
Output is correct |
36 |
Correct |
6 ms |
3620 KB |
Output is correct |
37 |
Correct |
6 ms |
3620 KB |
Output is correct |
38 |
Correct |
6 ms |
3624 KB |
Output is correct |
39 |
Correct |
6 ms |
3544 KB |
Output is correct |
40 |
Correct |
6 ms |
3628 KB |
Output is correct |
41 |
Correct |
6 ms |
3652 KB |
Output is correct |