题意:

市赛的题的弱化版,给定一个序列,你可以把任何数取-要么取+,问最小逆序数。

思路:

发现到这个题的性质,绝对值大的贡献只决定于绝对值小的,那么按照绝对值排序贪心即可,取左边贡献和右边贡献的min就行了。

代码:

// Problem: E. Jeff and Permutation
// Contest: Codeforces - Codeforces Round #204 (Div. 1)
// URL: https://codeforces.com/contest/351/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define IL inline
#define x first
#define y second
typedef long long ll;
using namespace std;
const int N=1000010;
int tr[N];
int n;
int lowbit(int x)
{
return x&-x;
}A
void add(int a,int b)
{
for(int i=a;i<=n;i+=lowbit(i))
tr[i]+=b;
}
int a[N];
int query(int x)
{
int res=0;
for(int i=x;i;i-=lowbit(i))
res+=tr[i];
return res;
}
struct node{
int x;
int pos;
bool operator<(const node&w)const
{
if(x!=w.x)
return x<w.x;
return pos<w.pos;
}
};
vector<node>v;
int cnt[N];
int main()
{

cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
a[i]=abs(a[i]);
v.push_back({a[i],i});
}
sort(v.begin(),v.end());
long long res=0;
for(int i=0;i<v.size();i++)
{
int x=v[i].x;
int pos=v[i].pos;
int l=query(pos-1);
int r=query(n)-query(pos);
l-=cnt[x];
cnt[x]++;
//cout<<l<<" "<<r<<endl;
res+=min(l,r);
add(pos,1);
}
cout<<res<<endl;
return 0;
}