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 int long long
const int MAXN = 1e5 + 10;
array<int, MAXN> bit;
void update(int id, int val)
{
while (id < MAXN)
{
bit[id] += val;
id += id&-id;
}
}
int query(int id)
{
int resp = 0;
while (id > 0)
{
resp += bit[id];
id -= id&-id;
}
return resp;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
string s;
cin >> s;
vector<vector<int>> freq(30);
for (int i = 0; i < 2*N; i++) freq[s[i]-'a'].push_back(i);
vector<int> v1(2*N);
vector<pair<int, int>> pares;
for (int c = 0; c < 30; c++)
{
int sz = freq[c].size();
for (int i = 0; i < sz/2; i++)
{
v1[freq[c][i]] = 0; v1[freq[c][i+sz/2]] = 1;
pares.emplace_back(freq[c][i], freq[c][i+sz/2]);
}
}
int resp = 0, cnt1 = 0;
for (int i = 0; i < 2*N; i++)
{
if (v1[i]) cnt1++;
else resp += cnt1;
}
sort(pares.rbegin(), pares.rend());
for (auto &[x, y] : pares)
{
resp += query(y);
update(y, +1);
}
cout << resp << '\n';
}
# | 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... |