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 <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
const long long inf = 1e18 + 7;
struct SEGT{
vector < int > t;
int tree_size;
const int identity_element = 0;
int merge(int x , int y){
return x + y;
}
void init(int x){
tree_size = x+3;
t.assign(2*tree_size , identity_element);
}
void modify(int p, int value) { // set value at position p
for (t[p += tree_size] = value; p > 1; p >>= 1){
t[p>>1] = merge(t[p] , t[p^1]);
}
}
int query(int l, int r) { // sum on interval [l, r]
int res = identity_element;
for (r+=1 , l += tree_size, r += tree_size; l < r; l >>= 1, r >>= 1) {
if (l&1) res = merge(res , t[l++]);
if (r&1) res = merge(res , t[--r]);
}
return res;
}
};
long long count_swaps(vector<int> s) {
int n = sz(s) / 2;
long long ans = inf;
// cout << "n : " << n << endl;
// for(auto itr : s)cout << itr << " ";cout << endl;
if(n == 1){// 10 pts - n = 1
ans = 0;
if(s[0] > 0)ans++;
return ans;
}
if(n <= 8){// 20 pts - n <= 8
vector < int > shoes;
for(auto itr : s)if(itr > 0)shoes.push_back(itr);
vector < int > perm(n);
iota(all(perm) , 0);
do{
vector < int > tmp = s;
long long locans = 0;
vector < int > news;
for(auto itr : perm){
news.push_back(-shoes[itr]);
news.push_back(shoes[itr]);
}
for(int i = 0;i<2*n;i++){
int cur = i;
while(tmp[cur] != news[i] and cur < (2 * n))cur++;
assert(cur != (2*n));
locans += cur - i;
int tmp_val = tmp[cur];
for(int j = cur;j>i;j--){
tmp[j] = tmp[j-1];
}
tmp[i] = tmp_val;
}
ans = min(ans , locans);
} while(next_permutation(all(perm)));
return ans;
}
set < int > ste;
for(auto itr : s){
ste.insert(abs(itr));
}
if(sz(ste) == 1){// 20 pts - all shoe sizes are same
ans = 0;
SEGT segt;
segt.init(2 * n + 5);
int sayac0 = 1 , sayac1 = 2;
for(int i = 0;i<2*n;i++){
if(s[i] < 0){
ans += segt.query(sayac0 , 2*n);
segt.modify(sayac0 , 1);
sayac0+=2;
}
else{
ans += segt.query(sayac1 , 2*n);
segt.modify(sayac1 , 1);
sayac1+=2;
}
}
return ans;
}
// else{// 15 pts - first n are left , rest are right and i = i+n
// }
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:70:14: warning: control reaches end of non-void function [-Wreturn-type]
70 | set < int > ste;
| ^~~
# | 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... |