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"shoes.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef unordered_set<ll> uset;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=0;i<=n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define all(x) x.begin(),x.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("unroll-loops")
#pragma gcc target("avx2,see4")
#define N 20
ll dat[N];
void add(ll i,ll x){
for(;i<N;i+=i&-i)dat[i]+=x;
}
ll sum(ll i){
ll res=0;
for(;i>0;i-=i&-i)res+=dat[i];
return res;
}
ll calc_tentou(vector<ll> p){
int n=p.size();
rep(i,n)p[i]++;
rep(i,N)dat[i]=0;
ll ans=0;
rep(i,n){
ans+=i-sum(p[i]);
add(p[i],1);
}
return ans;
}
ll count_swaps(vector<int> s){
int n=s.size()/2;
if(n>8)return 0;
vector<P> L,R;//左・右の靴の{サイズ、場所}
for(int i=0;i<2*n;i++){
if(s[i]<0)L.push_back(make_pair(-s[i],i));
else R.push_back(make_pair(s[i],i));
}
vector<ll> pl[N];//pl[x]:=サイズxの右の靴のある場所、後ろが小さい
for(auto e:R){
pl[e.first].push_back(e.second);
}
rep(i,N)reverse(all(pl[i]));
vector<ll> S(n),T(n);//(S[i],T[i])..場所の組(s[i],t[i])をuniteする
for(int i=0;i<n;i++){
S[i]=L[i].second;
T[i]=pl[L[i].first].back();
pl[L[i].first].pop_back();
}
//rep(i,n)cerr<<"pair:"<<S[i]<<" "<<T[i]<<endl;
ll ans=1e17;
vector<ll> jun;
rep(i,n)jun.push_back(i);
do{
vector<ll> p(2*n);//p[i]:=元々iにあった靴がいく場所
rep(i,n){
p[S[i]]=jun[i]*2;
p[T[i]]=jun[i]*2+1;
}
chmin(ans,calc_tentou(p));
//cerr<<"ten"<<calc_tentou(p)<<endl;
}while(next_permutation(all(jun)));
return ans;
}/*
int main(){
ll n;
cin>>n;
vector<int> s(2*n);
rep(i,2*n)cin>>s[i];
cout<<count_swaps(s)<<endl;
}*/
Compilation message (stderr)
shoes.cpp:17:0: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
#pragma gcc optimize("O3")
shoes.cpp:18:0: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
#pragma gcc optimize("unroll-loops")
shoes.cpp:19:0: warning: ignoring #pragma gcc target [-Wunknown-pragmas]
#pragma gcc target("avx2,see4")
# | 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... |