이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1<<17;
bool got[MAX*2];
int t[MAX*2];
set<int>a[MAX],b[MAX];
void update(int u,int c)
{
for (t[u+=MAX]=c;u>1;u>>=1)
t[u>>1]=t[u]+t[u^1];
}
int query(int u,int v)
{
int sum=0;
for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1)
{
if (u&1)sum+=t[u++];
if (v&1)sum+=t[--v];
}
return sum;
}
ll count_swaps(vi tab2)
{
ll ans=0;
int n=sz(tab2);
int tab[n+1];
for (int i=0;i<=n;i++)tab[i]=0;
for (int i=1;i<=n;i++)tab[i]=tab2[i-1];
for (int i=1;i<=n;i++)
{
if (tab[i]<0)b[-tab[i]].ins(i);
else a[tab[i]].ins(i);
}
for (int i=1;i<=n;i++)update(i,1);
for (int i=1;i<=n;i++)
{
if (got[i])continue;
got[i]=true;
if (tab[i]>0)
{
int wsk=*b[tab[i]].lower_bound(i);
b[tab[i]].erase(wsk);
got[wsk]=true;
ans+=query(1,wsk)-query(1,i);
update(wsk,0);
}
else
{
int wsk=*a[-tab[i]].lower_bound(i);
a[tab[i]].erase(wsk);
got[wsk]=true;
ans+=query(1,wsk)-query(1,i)-1;
update(wsk,0);
}
}
return ans;
}
/*
int main()
{
cout<<count_swaps({2, 1, -1, -2});
}
*/
# | 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... |