[Queued matches]

P1966

[Queued matches]

Effect Title: the title number of exchanges that the given minimum time and

practice:

1: To that \ (\ sum_ {i} ^ {n} (a_i-b_i) ^ {2} \) minimum, i.e. \ (a_i \) are ranked \ (I \) a large number of to and \ (B_i \) are ranked \ (I \) a large number of positions corresponding to

2: From 1 shows that the height of each title matches and it does not matter, he was only interested in the relative size in this sequence, so we open a new \ (Rank \) array, so that \ (a_i, b_i \) drained subsequent to each \ (a_i \) the original index location, \ (B_i \) original position value, that is, \ (Rank [a [i] ] = b [i] \)

3: So is the final state of each \ (= a_i B_i \) , i.e. \ (Rank [i] = i \)

4: So we put the topic a bit transformed, re-describe the topic: given to a random number sequence arranged in a minimum number of times the number of columns in ascending order, which is obviously reverse right.

Description:

1: Why is this thing in reverse for it? Because the meaning of the questions, each exchange two adjacent, so that the number of columns and orderly, then this is definitely the number of exchanges bubble sort it.

2: So why bubble sort of exchange and the number of times equal to reverse it?

prove:

1: We set the number as \ ({A_1, A_2, A_N A_3 ...} \) , the number of reverse number generated for each \ ({b_1, b_2, b_3 ... b_n} \)

2: We generated from a reverse for not \ (0 \) the number began to consider (as \ (0 \) before then explained that it had lined up), consider this number routed to the front makes this a location no number on the reverse, then switched to the back of each \ (a_i \) the number of pairs produced in reverse order (b_i \) \ will not have an impact, but this number is transferred to a location in front of him makes no reverse exchange of times equal to the number of reverse order. For chestnut: \ ((3451) \) : If the \ (1 \) transferred to the front obviously exchange \ (3 \) times. So each \ (a_i \) moved to the front position so that it does not produce a reverse of the contribution of the answers produced (b_i \) \ contribution, but before each number does not change the size of the position, and at the same time behind each \ (a_i \) number generated in reverse order of the \ (b_i \) will not have an impact, so the conclusion is proved.

So we now only requires a \ (Rank \) the number of arrays in the reverse order to

Merge sort

#include <cstdio>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int maxn = 1e5+6,mod = 99999997;
int n,Rank[maxn],Sort[maxn];LL ans;
struct qwq{
    LL val;int pos;
    bool operator < (const qwq &C)const
    {
        if(val != C.val)
            return val < C.val;
        else return pos < C.pos;
    }
}a[maxn],b[maxn];

void mergesort(int l,int r)
{
    if(l == r) return;
    int mid = (l + r) >> 1;
    mergesort(l,mid);mergesort(mid+1,r);
    int i = l,j = mid + 1,k = l;
    while(i <= mid && j <= r)
    {
        if(Rank[i] <= Rank[j])
            Sort[k++] = Rank[i++];
        else
        {
            Sort[k++] = Rank[j++];
            ans += (LL)mid - i + 1;
            ans %= mod;
        }
    }
    while(i <= mid)
        Sort[k++] = Rank[i++];
    while(j <= r)
        Sort[k++] = Rank[j++];
    for(int pos=l;pos<=r;pos++)
        Rank[pos] = Sort[pos];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i].val);
        a[i].pos = i;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&b[i].val);
        b[i].pos = i;
    }
    sort(a+1,a+n+1);sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        Rank[a[i].pos] = b[i].pos;
    mergesort(1,n);
    printf("%lld",ans);
    return 0;
}

Fenwick tree

#include <cstdio>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int maxn = 1e5+6,mod = 99999997;
int n,Rank[maxn];LL ans,tree[maxn];
struct qwq{
    LL val;int pos;
    bool operator < (const qwq &C)const
    {
        if(val != C.val)
            return val < C.val;
        else return pos < C.pos;
    }
}a[maxn],b[maxn];

int lowbit(int k)
{
    return k & -k;
}

void add(int x,int k)
{
    for(;x<=n;x+=lowbit(x))
        tree[x] += k;
    return ;
}

LL sum(int x)
{
    LL res = 0;
    for(;x;x-=lowbit(x))
        res += tree[x];
    return res;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i].val);
        a[i].pos = i;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b[i].val);
        b[i].pos = i;
    }
    sort(a+1,a+n+1);sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        Rank[a[i].pos] = b[i].pos;
    for(int i=1;i<=n;i++)
    {
        add(Rank[i],1);
        ans += i - sum(Rank[i]);
        ans %= mod;
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/-Wind-/p/11847596.html