이 제출은 이전 버전의 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;
vi pos[MAX*2];
bool got[MAX*2];
int t[MAX*2];
void update(int u,int v,int c)
{
for (u+=MAX,v+=MAX;u<v;u>>=1,v>>=1)
{
if (u&1)t[u++]+=c;
if (v&1)t[--v]+=c;
}
}
int query(int u)
{
int sum=0;
for (u+=MAX;u>0;u>>=1)sum+=t[u];
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=n;i>=1;i--)
{
if (tab[i]<0)tab[i]=-tab[i]+n;
pos[tab[i]].pb(i);
}
for (int i=1;i<=n*2;i++)
{
//cout<<"ZAWARTOSC "<<i<<"\n";
//for (auto it:pos[i])cout<<it<<" ";
//cout<<"\n";
}
for (int i=1;i<=n-1;i++)
{
if (got[i])continue;
got[i]=true;
int wsk=tab[i];
if (wsk<n)wsk+=n;
else wsk-=n;
while (pos[wsk].back()<i)pos[wsk].pop_back();
got[pos[wsk].back()]=true;
int x=pos[wsk].back();
ans+=x-i-1+(wsk>tab[i])-query(x)+query(i-1);
update(x,n*2,1);
}
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... |