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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
long long int typedef li;
int n;
int fwt[200005];
void add(int i)
{
for (;i<=2*n;i+=(i&-i)) fwt[i]++;
}
int pre(int i)
{
li ret=0;
for (;i;i-=(i&-i)) ret+=fwt[i];
return ret;
}
int gde[200005];
li invcount(int n)
{
li ans=0;
//for (int i=0;i<2*n;i++) cout<<gde[i]<<" "; cout<<endl;
for (int i=0;i<2*n;i++)
{
ans+=i-pre(gde[i]);
//cout<<i<<": "<<pre(gde[i])<<endl;
//for (int i=1;i<=2*n;i++) cout<<fwt[i]<<","; cout<<endl<<endl;
add(gde[i]);
}
return ans;
}
int cntpoz[100005];
int cntneg[100005];
li count_swaps(vector<int> s)
{
n=s.size()/2;
vector<int> pom; map<int,vector<int>> poz;
for (int i=0;i<2*n;i++)
{
if (s[i]>0)
{
if (cntpoz[s[i]]) cntpoz[s[i]]--;
else cntneg[s[i]]++,pom.push_back(-s[i]),pom.push_back(s[i]);
}
else
{
if (cntneg[-s[i]]) cntneg[-s[i]]--;
else cntpoz[-s[i]]++,pom.push_back(s[i]),pom.push_back(-s[i]);
}
}
for (int i=0;i<2*n;i++) poz[s[i]].push_back(i);
for (int i=2*n-1;i>=0;i--) gde[i]=poz[pom[i]].back()+1,poz[pom[i]].pop_back();
return invcount(n);
}
/*
2
1 2 -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... |