이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "shoes.h"
#define lowbit(x) (x&(-x))
#define inf 1000000007
#define mod 1000000007
#define rnd() rand_num()
#define bigrnd() dis(rand_num)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
//#define int long long
using namespace std;
unsigned sed=std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(sed);
uniform_int_distribution<long long> dis(0,inf);
template <typename T> void read(T &x){
x=0;char ch=getchar();int fh=1;
while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
x*=fh;
}
template <typename T> void write(T x) {
if (x<0) x=-x,putchar('-');
if (x>9) write(x/10);
putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int n;
int s[200005];
set<int> mp[100005];
set<int> st;
int id[200005];
int bit[200005];
void modify(int x,int y)
{
while(x<=2*n)
{
bit[x]+=y;
x+=lowbit(x);
}
}
int query(int x)
{
int res=0;
while(x)
{
res+=bit[x];
x-=lowbit(x);
}
return res;
}
long long count_swaps(vector<int> S)
{
n=S.size()/2;
for(int i=1;i<=2*n;++i)
{
s[i]=S[i-1];
if(s[i]>0) mp[s[i]].insert(i);
else st.insert(i);
}
if(n<=8)
{
for(int i=1;i<=2*n;++i)
id[i]=i;
long long ans=inf;
do
{
bool flag=true;
for(int i=1;i<=2*n;++i)
{
if(i%2==1)
{
if(s[id[i]]>0)
{
flag=false;
break;
}
}
else
{
if(s[id[i-1]]+s[id[i]]!=0)
{
flag=false;
break;
}
}
}
if(!flag) continue;
long long res=0;
for(int i=2*n;i>=1;--i)
{
res+=query(id[i]);
modify(id[i],1);
}
ans=min(ans,res);
for(int i=2*n;i>=1;--i)
{
modify(id[i],-1);
}
}
while(next_permutation(id+1,id+2*n+1));
return ans;
}
for(int i=1;i<=2*n;++i)
{
if(i%2==1)
{
id[i]=*(st.begin());
st.erase(st.begin());
}
else
{
id[i]=*(mp[-s[id[i-1]]].begin());
mp[-s[id[i-1]]].erase(mp[-s[id[i-1]]].begin());
}
}
long long ans=0;
for(int i=2*n;i>=1;--i)
{
ans+=query(id[i]);
modify(id[i],1);
}
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... |