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 <cmath>
#include <cassert>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#include "shoes.h"
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
using namespace std;
template<class T> struct Seg { // comb(ID,b) = b
const T ID = 0; T comb(T a, T b) { return a+b; }
int n; vector<T> seg;
void init(int _n) { n = _n; seg.assign(2*n,ID); }
void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(int p, T val) { // set val at position p
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(int l, int r) { // sum on interval [l, r]
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
int64_t get_inversions(vector<int> v) {
Seg<int> st;
st.init(v.size() + 3);
int64_t ans = 0;
for (int i = 0; i < v.size(); i++) {
ans += st.query(v[i] + 1, v.size() + 2);
st.upd(v[i], st.query(v[i], v[i]) + 1);
}
return ans;
}
long long count_swaps(vector<int> v) {
map<int, set<int>> myMap;
for (int i = 0; i < v.size(); i++) {
myMap[v[i]].insert(i);
}
vector<int> vec(v.size());
int cntr = 0;
for (int i = 0; i < v.size(); i++) {
if (myMap[-v[i]].empty()) continue;
if (myMap[v[i]].count(i) == false) continue;
int x = *myMap[-v[i]].begin();
myMap[-v[i]].erase(x);
myMap[v[i]].erase(i);
vec[i] = cntr++;
vec[x] = cntr++;
if (v[i] > 0) {
swap(vec[i], vec[x]);
}
}
return get_inversions(vec);
}
/*
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << count_swaps({-1,1,-1,-1,-1,1,1,1});
}
*/
Compilation message (stderr)
shoes.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
16 | #pragma GCC optimization ("O3")
|
shoes.cpp:17: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
17 | #pragma GCC optimization ("Ofast")
|
shoes.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
18 | #pragma GCC optimization ("unroll-loops")
|
shoes.cpp: In function 'int64_t get_inversions(std::vector<int>)':
shoes.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
shoes.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |