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>
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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |