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>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<ll, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+2;
const int M = 52;
const int mod = 1e9+7;
const ll base = 75;
const ll inf = 1e16;
int n, lab[N], t, c[N], h[N], m, a[N], b[N], d[N], k;
ll ans;
vector<int> adj[N], g[N];
vector<int> vi;
string s;
bool vis[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void add(int& x, int y)
{
x += y;
if(x >= mod)x -= mod;
}
int findp(int u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(int u, int v)
{
u = findp(u);
v = findp(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
struct BIT
{
int n;
vector<ll> fe;
BIT(){}
void init(int _n)
{
n = _n;
fe.resize(n+1, 0);
}
void reset()
{
fill(fe.begin(), fe.end(), 0);
}
void add(int id, int x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
ll get(int id)
{
ll total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
};
void sol()
{
cin >> n >> s;
for(int i = 1; i <= n*2; i ++)
adj[s[i-1]-'a'].pb(i);
for(int p = 0; p < 26; p ++)
{
if(adj[p].empty())continue;
m = adj[p].size();
for(int i = 0; i < m/2; i ++)vi.pb(adj[p][i]);
}
BIT bit;
bit.init(n*2);
sort(vi.begin(), vi.end());
for(int i = 0; i < n; i ++)
{
ans += bit.get(n*2) - bit.get(vi[i]);
bit.add(vi[i], 1);
g[s[vi[i]-1]-'a'].pb(i+1);
}
vi.clear();
for(int p = 0; p < 26; p ++)
{
if(adj[p].empty())continue;
m = adj[p].size();
for(int i = 0; i < m/2; i ++)vi.pb(adj[p][i+m/2]);
}
sort(vi.begin(),vi.end());
for(int i = 0; i < n; i ++)
{
ans += bit.get(n*2) - bit.get(vi[i]);
bit.add(vi[i], 1);
g[s[vi[i]-1]-'a'].pb(i+n+1);
}
for(int i = 0; i < 26; i ++)
{
if(g[i].empty())continue;
m = g[i].size();
for(int j = m/2; j < m; j ++)
{
a[g[i][j]-n] = g[i][j-m/2];
//cout << g[i][j-m/2] <<" "<< g[i][j]-n << '\n';
}
}
bit.reset();
for(int i = 1; i <= n; i ++)
{
ans += bit.get(n) - bit.get(a[i]);
bit.add(a[i], 1);
}
cout << ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
/*
1234
21
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(task".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... |