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 <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 (stderr)
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;
| ^~~
# | 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... |