# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
144549 | daniel920712 | Arranging Shoes (IOI19_shoes) | C++14 | 440 ms | 32572 KiB |
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 <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <assert.h>
#include <map>
#include <deque>
using namespace std;
struct A
{
int l,r;
int nxl,nxr;
int how;
}Node[1000005];
struct B
{
int x,y;
}all[1000005];
int a[200005];
int b[200005];
int now=1;
map < int , vector < int > > all3;
void build(int l,int r,int here)
{
Node[here].l=l;
Node[here].r=r;
Node[here].how=0;
if(l==r) return;
Node[here].nxl=now++;
Node[here].nxr=now++;
build(l,(l+r)/2,Node[here].nxl);
build((l+r)/2+1,r,Node[here].nxr);
}
void change(int l,int r,int here)
{
if(l==Node[here].l&&r==Node[here].r)
{
Node[here].how++;
return;
}
if(r<=(Node[here].l+Node[here].r)/2) change(l,r,Node[here].nxl);
else if(l>(Node[here].l+Node[here].r)/2) change(l,r,Node[here].nxr);
else
{
change(l,(Node[here].l+Node[here].r)/2,Node[here].nxl);
change((Node[here].l+Node[here].r)/2+1,r,Node[here].nxr);
}
}
int Find(int where,int here)
{
if(where==Node[here].l&&where==Node[here].r) return Node[here].how;
if(where<=(Node[here].l+Node[here].r)/2) return Find(where,Node[here].nxl)+Node[here].how;
else if(where>(Node[here].l+Node[here].r)/2) return Find(where,Node[here].nxr)+Node[here].how;
}
bool F(B a,B b)
{
int x=0,y=0;
x=a.x+a.y;
y=b.x+b.y;
if(a.x>a.y) x++;
if(b.x>b.y) y++;
return x<y||x==y&&min(a.x,a.y)<min(b.x,b.y);
}
long long count_swaps(std::vector<int> S)
{
long long ans1=0,small,what,t,a,b;
int i,j,k,N=S.size(),x=0,M;
build(0,N-1,0);
for(i=0;i<N;i++)
{
all3[S[i]].push_back(i);
//printf("%d %d %d\n",i,S[i],all3[S[i]].back());
}
for(i=1;i<=N/2;i++)
{
M=all3[i].size();
for(j=0;j<M;j++)
{
all[x].x=all3[0-i][j];
all[x].y=all3[i][j];
//printf("%d %d %d %d\n",i,j,all3[i][j],all3[0-i][j]);
x++;
}
}
sort(all,all+N/2,F);
for(i=0;i<N/2;i++)
{
//printf("%d %d\n",all[i].x,all[i].y);
ans1+=Find(all[i].x,0)+Find(all[i].y,0)+all[i].x+all[i].y-i*2-i*2-1;
if(all[i].x>all[i].y) ans1++;
change(0,all[i].x,0);
change(0,all[i].y,0);
}
return ans1;
}
Compilation message (stderr)
# | 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... |