#include <iostream>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <map>
#include <deque>
#include <stdio.h>
using namespace std;
map<char,vector<int>> pos (string s) {
map<char,vector<int>> m;
for (int i = 0; i < s.size(); i++) {
m[s[i]].push_back(i);
}
return m;
}
struct SegmentTree {
vector<int> vec;
int query (int dum, int tl, int tr, int l, int r) {
if (tl >= l and tr <= r) {
return vec[dum];
}
if (r < tl or l > tr) {
return 0;
}
return query(2 * dum + 1, tl, (tl + tr)/2, l, r) + query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r);
}
int query (int l, int r) {
return query(0, 0, (int)vec.size()/2 - 1, l, r);
}
void upd (int x, int y) {
x += (int)vec.size()/2 - 1;
vec[x] += y;
while (x != 0) {
x = (x - 1)/2;
vec[x] = vec[2 * x + 1] + vec[2 * x + 2];
}
}
SegmentTree (int n) {
n = (1 << ((int)log2(n) + 1));
vec.assign(2 * n, 0);
}
};
int inversions (vector<int> v) {
SegmentTree st(v.size());
int ans = 0;
for (int i = 0; i < v.size(); i++) {
st.upd(v[i], 1);
ans += st.query(v[i] + 1, (int)v.size() - 1);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
string str;
map<int,int> oc_s;
for (int i = 0; i < s.size(); i++) {
oc_s[s[i]]++;
}
map<char,int> oc;
string g;
for (int i = 0; i < s.size(); i++) {
oc[s[i]]++;
if (oc[s[i]] <= oc_s[s[i]]/2) {
str += s[i];
} else {
g += s[i];
}
}
auto p1 = pos(s);
auto p2 = pos(str + str);
int ans = 0;
vector<int> perm(2 * n);
for (auto& p: p1) {
for (int i = 0; i < p1[p.first].size(); i++) {
perm[p2[p.first][i]] = p1[p.first][i];
}
}
cout << inversions(perm) << '\n';
}
Compilation message
Main.cpp: In function 'std::map<char, std::vector<int> > pos(std::string)':
Main.cpp:17:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int inversions(std::vector<int>)':
Main.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int i = 0; i < p1[p.first].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:82:9: warning: unused variable 'ans' [-Wunused-variable]
82 | int ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
17 ms |
2704 KB |
Output is correct |
3 |
Correct |
37 ms |
5340 KB |
Output is correct |
4 |
Incorrect |
49 ms |
6624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
17 ms |
2704 KB |
Output is correct |
3 |
Correct |
37 ms |
5340 KB |
Output is correct |
4 |
Incorrect |
49 ms |
6624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
17 ms |
2704 KB |
Output is correct |
3 |
Correct |
37 ms |
5340 KB |
Output is correct |
4 |
Incorrect |
49 ms |
6624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
17 ms |
2704 KB |
Output is correct |
3 |
Correct |
37 ms |
5340 KB |
Output is correct |
4 |
Incorrect |
49 ms |
6624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |