#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 1e5 + 5;
vector<int> occ[26];
vector<int> leftV, rightV;
ll fen[MAXN];
void update(int k) {
while (k < MAXN) {
fen[k]++;
k += k&-k;
}
}
ll query(int k) {
ll res = 0;
while (k >= 1) {
res += fen[k];
k -= k&-k;
}
return res;
}
int main() {
fast
int n;
string s;
cin >> n >> s;
s = "#" + s;
for (int i = 1; i <= 2*n; i++)
occ[s[i]-'a'].push_back(i);
for (int i = 0; i < 26; i++) {
int q = occ[i].size() / 2;
for (int j = 0; j < q; j++)
leftV.push_back(occ[i][j]);
for (int j = q; j < 2*q; j++)
rightV.push_back(occ[i][j]);
}
sort(leftV.begin(), leftV.end());
sort(rightV.begin(), rightV.end());
for (auto u : rightV)
leftV.push_back(u);
ll ans = 0;
for (int i = 0; i < 2*n; i++) {
ans += i - query(leftV[i]);
update(leftV[i]);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Runtime error |
8 ms |
5472 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Runtime error |
8 ms |
5472 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Runtime error |
8 ms |
5472 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Runtime error |
8 ms |
5472 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |