# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
341757 | ogibogi2004 | Arranging Shoes (IOI19_shoes) | C++14 | 101 ms | 19820 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<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=2e5+6;
int fen[MAXN];
void update(int idx,int val)
{
idx++;
for(;idx<MAXN;idx+=idx&(-idx))
{
fen[idx]+=val;
}
}
int query(int idx)
{
idx++;
int ret=0;
for(;idx>0;idx-=idx&(-idx))
{
ret+=fen[idx];
}
return ret;
}
vector<int>shoes[MAXN][2];
bool vis[MAXN];
long long count_swaps(vector<int> s) {
for(int i=0;i<s.size();++i)
{
if(s[i]>0)shoes[s[i]][0].push_back(i);
else shoes[-s[i]][1].push_back(i);
}
for(int i=0;i<MAXN;i++)
{
reverse(shoes[i][0].begin(),shoes[i][0].end());
reverse(shoes[i][1].begin(),shoes[i][1].end());
}
for(int i=0;i<s.size();++i)
{
update(i,+1);
}
ll ans=0;
for(int i=0;i<s.size();++i)
{
if(vis[i])continue;
if(s[i]<0)
{
int k=shoes[-s[i]][0].back();
ans+=(ll)query(k)-query(i)-1;
update(i,+1);
vis[k]=1;
update(k+1,-1);
shoes[-s[i]][0].pop_back();
shoes[-s[i]][1].pop_back();
}
else
{
int k=shoes[s[i]][1].back();
ans+=(ll)query(k)-query(i);
update(i,+1);
update(k+1,-1);
vis[k]=1;
shoes[s[i]][1].pop_back();
shoes[s[i]][0].pop_back();
}
}
return ans;
}
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... |