树状数组+逆序对

E2. Array Optimization by Deque

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.

You are given an integer array a[1…n]=[a1,a2,…,an]a[1…n]=[a1,a2,…,an].

Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements [3,4,4][3,4,4] currently in the deque, adding an element 11 to the beginning will produce the sequence [1,3,4,4][1,3,4,4], and adding the same element to the end will produce [3,4,4,1][3,4,4,1].

The elements of the array are sequentially added to the initially empty deque, starting with a1a1 and finishing with anan. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end.

For example, if we consider an array a=[3,7,5,5]a=[3,7,5,5], one of the possible sequences of actions looks like this:

 1.add 33 to the beginning of the deque:deque has a sequence [3][3] in it;
 2.add 77 to the end of the deque:deque has a sequence [3,7][3,7] in it;
 3.add 55 to the end of the deque:deque has a sequence [3,7,5][3,7,5] in it;
 4.add 55 to the beginning of the deque:deque has a sequence [5,3,7,5][5,3,7,5] in it;

Find the minimal possible number of inversions in the deque after the whole array is processed.

An inversion in sequence dd is a pair of indices (i,j)(i,j) such that i<ji<j and di>djdi>dj. For example, the array d=[5,3,7,5]d=[5,3,7,5] has exactly two inversions — (1,2)(1,2) and (3,4)(3,4), since d1=5>3=d2d1=5>3=d2 and d3=7>5=d4d3=7>5=d4.

Input

The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

The first line of each test case description contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — array size. The second line of the description contains nn space-separated integers aiai (−109≤ai≤109−109≤ai≤109) — elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

Print tt lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm.

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int tr[N];int a[N];
vector<int> v;
typedef long long ll;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int c)
{
    for(int i=x;i<N;i+=lowbit(i)) tr[i]+=c;
}
int sum(int x)
{
    int res=0;
    for(int i=x;i;i-=lowbit(i)) res+=tr[i];
    return res;
}
int find(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
void solve()
{
    memset(tr,0,sizeof tr);
    v.clear();
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    ll res=0;
    for(int i=1;i<=n;i++)
    {
        int pos=find(a[i]);
        //cout<<pos<<endl;
        int num0=sum(N-1)-sum(pos),num1=sum(pos-1);
        res+=min(num0,num1);
        add(pos,1);
    }
    cout<<res<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值