This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: Yassine BenYounes
TASK: time
LANG: C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;*/
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
ll modd(ll x, ll n){while(x < 0){x += n;}return (x % n);} // modulo for negative numbers
int dx[8] = {0, -1, 0, 1, 1, 1, -1, -1};
int dy[8] = {-1, 0, 1, 0, -1, 1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 1e6+9;
const ll mod = 1e9+7;
const int inf = 1e9;
ll tree[mx];
ll sum_query(int node, int q_low, int q_high, int l, int r){
if(l >= q_low && r <= q_high){
return tree[node];
}
if(r < q_low || l > q_high){
return 0;
}
return sum_query(2*node, q_low, q_high, l, (l+r)/2) + sum_query(2*node+1, q_low, q_high, (l+r)/2 + 1, r);
}
int _count(int n){
int c = 0;
while(n > 0){
c+= (n&1);
n/=2;
}
return c;
}
int n;
void update(int ind, int v){
tree[n + ind] += v;
for(int j = (n+ind)/2; j >= 1;j/=2){
tree[j] = tree[j*2] + tree[j*2+1];
}
}
long long count_swaps(std::vector<int> S){
ll ans = 0;
S.insert(S.begin(), 0);
n = S.size();
map<int, queue<int>> mp;
for(int i = 1; i < S.size();i++){
mp[S[i]].push(i);
}
while(_count(n) != 1){
n++;
}
for(int i = 1; i < n;i++){
update(i, 1);
}
vector<bool> vis(S.size()+10, 0);
for(int i = 1; i < S.size();i++){
if(vis[i])continue;
int ind = mp[-S[i]].front();
ans += sum_query(1, 0, ind, 0,n-1) - sum_query(1, 0, i, 0,n-1) - 1;
mp[S[i]].pop();
mp[-S[i]].pop();
vis[i] = 1;
vis[ind] = 1;
update(i, 1);
update(ind, -1);
if(S[i] > 0)ans++;
}
return ans;
}
/*
int main(){
//ofstream fout ("teamwork.out");
//ifstream fin ("teamwork.in");
init();
speed;
cout << count_swaps({-2, 2, 2, -2, -2, 2}) << endl;
}*/
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
*/
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i = 1; i < S.size();i++){
| ~~^~~~~~~~~~
shoes.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 1; i < S.size();i++){
| ~~^~~~~~~~~~
shoes.cpp: In function 'void init()':
shoes.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
shoes.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |