#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
13 ms |
11432 KB |
Output is correct |
3 |
Correct |
18 ms |
13096 KB |
Output is correct |
4 |
Correct |
25 ms |
14216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
13 ms |
11432 KB |
Output is correct |
3 |
Correct |
18 ms |
13096 KB |
Output is correct |
4 |
Correct |
25 ms |
14216 KB |
Output is correct |
5 |
Correct |
5 ms |
9716 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9724 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
13 ms |
11432 KB |
Output is correct |
3 |
Correct |
18 ms |
13096 KB |
Output is correct |
4 |
Correct |
25 ms |
14216 KB |
Output is correct |
5 |
Correct |
5 ms |
9724 KB |
Output is correct |
6 |
Correct |
30 ms |
14280 KB |
Output is correct |
7 |
Correct |
28 ms |
14244 KB |
Output is correct |
8 |
Correct |
37 ms |
14248 KB |
Output is correct |
9 |
Correct |
37 ms |
14264 KB |
Output is correct |
10 |
Correct |
36 ms |
14216 KB |
Output is correct |
11 |
Correct |
30 ms |
14084 KB |
Output is correct |
12 |
Correct |
32 ms |
14160 KB |
Output is correct |
13 |
Correct |
30 ms |
14060 KB |
Output is correct |
14 |
Correct |
29 ms |
14032 KB |
Output is correct |
15 |
Correct |
31 ms |
13992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9624 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9756 KB |
Output is correct |
5 |
Correct |
6 ms |
9724 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
13 ms |
11432 KB |
Output is correct |
3 |
Correct |
18 ms |
13096 KB |
Output is correct |
4 |
Correct |
25 ms |
14216 KB |
Output is correct |
5 |
Correct |
5 ms |
9716 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9724 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9724 KB |
Output is correct |
10 |
Correct |
30 ms |
14280 KB |
Output is correct |
11 |
Correct |
28 ms |
14244 KB |
Output is correct |
12 |
Correct |
37 ms |
14248 KB |
Output is correct |
13 |
Correct |
37 ms |
14264 KB |
Output is correct |
14 |
Correct |
36 ms |
14216 KB |
Output is correct |
15 |
Correct |
30 ms |
14084 KB |
Output is correct |
16 |
Correct |
32 ms |
14160 KB |
Output is correct |
17 |
Correct |
30 ms |
14060 KB |
Output is correct |
18 |
Correct |
29 ms |
14032 KB |
Output is correct |
19 |
Correct |
31 ms |
13992 KB |
Output is correct |
20 |
Correct |
6 ms |
9624 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
6 ms |
9756 KB |
Output is correct |
24 |
Correct |
6 ms |
9724 KB |
Output is correct |
25 |
Correct |
5 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9724 KB |
Output is correct |
31 |
Correct |
6 ms |
9724 KB |
Output is correct |
32 |
Correct |
33 ms |
14344 KB |
Output is correct |
33 |
Correct |
29 ms |
14268 KB |
Output is correct |
34 |
Correct |
33 ms |
14220 KB |
Output is correct |
35 |
Correct |
33 ms |
14196 KB |
Output is correct |
36 |
Correct |
34 ms |
14092 KB |
Output is correct |
37 |
Correct |
32 ms |
14256 KB |
Output is correct |
38 |
Correct |
38 ms |
14108 KB |
Output is correct |
39 |
Correct |
33 ms |
14156 KB |
Output is correct |
40 |
Correct |
30 ms |
14044 KB |
Output is correct |
41 |
Correct |
30 ms |
14004 KB |
Output is correct |