#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<int64_t> vec;
int64_t 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);
}
int64_t 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);
}
};
int64_t inversions (vector<int> v) {
SegmentTree st(v.size());
int64_t 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 'int64_t 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 |
19 ms |
3932 KB |
Output is correct |
3 |
Correct |
38 ms |
7240 KB |
Output is correct |
4 |
Correct |
50 ms |
8472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
19 ms |
3932 KB |
Output is correct |
3 |
Correct |
38 ms |
7240 KB |
Output is correct |
4 |
Correct |
50 ms |
8472 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
19 ms |
3932 KB |
Output is correct |
3 |
Correct |
38 ms |
7240 KB |
Output is correct |
4 |
Correct |
50 ms |
8472 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
76 ms |
8576 KB |
Output is correct |
7 |
Correct |
87 ms |
8620 KB |
Output is correct |
8 |
Correct |
77 ms |
8648 KB |
Output is correct |
9 |
Correct |
73 ms |
8472 KB |
Output is correct |
10 |
Correct |
73 ms |
8544 KB |
Output is correct |
11 |
Correct |
72 ms |
8544 KB |
Output is correct |
12 |
Correct |
71 ms |
8548 KB |
Output is correct |
13 |
Correct |
71 ms |
8468 KB |
Output is correct |
14 |
Correct |
69 ms |
8408 KB |
Output is correct |
15 |
Correct |
75 ms |
8452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
19 ms |
3932 KB |
Output is correct |
3 |
Correct |
38 ms |
7240 KB |
Output is correct |
4 |
Correct |
50 ms |
8472 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
76 ms |
8576 KB |
Output is correct |
11 |
Correct |
87 ms |
8620 KB |
Output is correct |
12 |
Correct |
77 ms |
8648 KB |
Output is correct |
13 |
Correct |
73 ms |
8472 KB |
Output is correct |
14 |
Correct |
73 ms |
8544 KB |
Output is correct |
15 |
Correct |
72 ms |
8544 KB |
Output is correct |
16 |
Correct |
71 ms |
8548 KB |
Output is correct |
17 |
Correct |
71 ms |
8468 KB |
Output is correct |
18 |
Correct |
69 ms |
8408 KB |
Output is correct |
19 |
Correct |
75 ms |
8452 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
78 ms |
8576 KB |
Output is correct |
33 |
Correct |
80 ms |
8592 KB |
Output is correct |
34 |
Correct |
74 ms |
8628 KB |
Output is correct |
35 |
Correct |
76 ms |
8576 KB |
Output is correct |
36 |
Correct |
69 ms |
8548 KB |
Output is correct |
37 |
Correct |
74 ms |
8568 KB |
Output is correct |
38 |
Correct |
74 ms |
8592 KB |
Output is correct |
39 |
Correct |
72 ms |
8428 KB |
Output is correct |
40 |
Correct |
71 ms |
8440 KB |
Output is correct |
41 |
Correct |
73 ms |
8448 KB |
Output is correct |