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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
//#include "shoes.h"
long long count_swaps(std::vector<int> S){
ll ans = 0;
ll m = S.size();
vll exo[m+5];
f(i,0,m){
exo[abs(S[i])].pb(S[i]);
}
f(i,0,m){
vll s;
set<ll>p,pll;
ll pos = 0;
for(auto x:exo[i]){
s.pb(x);
if(x < 0){
pll.insert(pos);
}
else{
p.insert(pos);
}
pos++;
}
ll n = s.size();
f(i,0,n){
if(s[i] < 0 && s[i+1] > 0){
}
else if(s[i] > 0 && s[i+1] < 0){
ans++;
p.erase(i);
pll.erase(i+1);
p.insert(i+1);
pll.insert(i);
}
else if(s[i] < 0 && s[i+1] < 0){
ll f;
for(auto x:p){
f = x;
p.erase(f);
pll.insert(f);
pll.erase(i+1);
p.insert(i+1);
swap(s[i+1],s[f]);
break;
}
ans += f - (i+1);
}
else{
ll f;
for(auto x:pll){
f = x;
pll.erase(f);
p.insert(f);
p.erase(i);
pll.insert(i);
swap(s[i],s[f]);
break;
}
ans += f - i;
}
p.erase(i+1);
pll.erase(i);
i++;
}
}
return ans;
}
# | 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... |