Problem solving [NOIP2013_ match sorting] - problems and transformation

Problem solution_ NOIP2013_ Match sorting

Meaning:

  there are two sequences a and B, and the elements of each sequence are different. Any two adjacent elements of any sequence can be interchanged, and it is required to operate the two sequences so that ∑ ( a i − b i ) 2 \sum(a_i-b_i)^2 Σ (ai − bi) 2 is the minimum, and the minimum number of operations is calculated.

Solution:

  for these two sequences, what we are required to minimize is ∑ ( a i − b i ) 2 \sum(a_i-b_i)^2 ∑(ai​−bi​)2
  expand this equation to get ∑ ( a i 2 − 2 a i b i + b i 2 ) \sum(a_i^2-2a_ib_i+b_i^2) Σ (ai2 − 2ai bi + bi2)   that is ∑ ( a i 2 + b i 2 ) − ∑ ( 2 a i b i ) \sum(a_i^2+b_i^2)-\sum(2a_ib_i) ∑ (ai2 + bi2) − ∑ (2ai bi)   because a i , b i a_i,b_i ai, bi   are fixed values, so we only need   to maximize ∑ ( 2 a i b i ) \sum(2a_ib_i) Σ (2ai, bi) is sufficient.

  for two sequences, it is better when all sequences are in ascending / descending order than when they are out of order ∑ ( 2 a i b i ) \sum(2a_ib_i) Σ (2ai, bi) is large. This is easy to prove, which can be transformed into area or proved by mathematical induction.

  another obvious point is that for sequences a and B, the same operation is performed on their corresponding elements, that is, "some operation together" has no effect on the answer.

  then, the problem is that for sequences a and b, find the minimum number of operations on b, so that after some operations are performed on the b 'sequence generated after b' and a "together", so that after a becomes ascending, the corresponding size positions of the elements of a and b 'are equal (the size position is the position of an element after a sequence is sorted).

  well, let's first discretize the A and b sequences. The elements in the sequence are mapped to the subscripts of the corresponding elements, and let a map to c and B map to d. Then, right? c is sorted based on a, and then D is sorted based on B. A new sequence c ', d' is obtained. At this time, these two sequences save the new positions of the original elements when a and B are changed into ascending order respectively. Then, according to the above, when a and B "do some operations together", it has no effect on the answer. We have successfully transformed the problem into: only the adjacent elements in the sequence can be exchanged, so that the minimum exchange times of d '= c'. But we still can't solve it easily.

  so, let's sort c, and then sort d 'based on C'. The new sequences C '' and d 'obtained at this time are equivalent to performing some operations to make d' when C 'is ascending. At this step, we will find the minimum number of exchanges that can only exchange adjacent elements in the sequence, so that d '= C'.

  according to the linear algebra teacher:

  • Exchange only two adjacent numbers to make them ascending: the number of exchanges is the inverse logarithm
  • It is proved that considering an arrangement, first move its maximum number to the end of the sequence, and the number is the inverse logarithm of the maximum number and the number after it. At this time, the last bit has been determined, and then consider the arrangement of the first n-1 numbers. Finally, the number of operations of the whole sequence is the inverse logarithm.

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+7,P=1e8-3;
template <typename _TP>
inline _TP read(_TP &X){
    char ch=0;int w;X=0;
    while(!isdigit(ch)){w=ch=='-';ch=getchar();}
    while(isdigit(ch)){X=(X<<1)+(X<<3)+(ch^48);ch=getchar();}
    X=w?-X:X;
    return X;
}
long long a[MAXN],b[MAXN],t[MAXN],pos[MAXN],w[MAXN];
inline bool cmpA(const int x,const int y){
    return a[x]<a[y];
}
inline bool cmpB(const int x,const int y){
    return b[x]<b[y];
}
long long cnt=0;
void merge_sort(const int l,const int r){
    if(r-l>1){
        int m=l+(r-l)/2;
        int p=l,q=m,i=l;
        merge_sort(l,m);
        merge_sort(m,r);
        while(p<m || q<r){
            if(q>=r || (p<m && w[p]<=w[q])){
                t[i++]=w[p++];
            }
            else {
                t[i++]=w[q++];
                cnt+=m-p;
            }
        }
        for(int i=l;i<r;i++){
            w[i]=t[i];
        }
    }
}
int c[MAXN],d[MAXN];
int main(){
    long long n;
    read(n);
    for(int i=1;i<=n;i++){
        read(a[i]);
        c[i]=i;
    }
    for(int i=1;i<=n;i++){
        read(b[i]);
        d[i]=i;
    }
    sort(c+1,c+n+1,cmpA);
    sort(d+1,d+n+1,cmpB);
    for(int i=1;i<=n;i++){
        w[c[i]]=d[i];
    }
    merge_sort(1,n+1);
    cout<<cnt%(P);
    return 0;
}
/*
About sequence operations:
    1.Swap only two adjacent numbers to make them ascending:
    The number of exchanges is in reverse order logarithm
    You need to consult your linear algebra teacher:
    Consider an arrangement, first move its maximum number to the end of the sequence, and the number is the inverse logarithm of the maximum number and the number after it
    At this time, the last bit has been determined, and then consider the arrangement of the first n-1 numbers
    2.Swapping any two numbers makes the sequence ascending
    The number of exchanges is the number of n-cycles
    Define a cycle as 3 at 5, 5 at 8, and 8 at 3
*/

Tags: Algorithm data structure linear algebra

Posted on Fri, 08 Oct 2021 04:03:41 -0400 by standalone