Luo Gu -1966 matches queue

Title Description
Han Han matches have two boxes, each box containing n number of matches, each match has a height. Now the pack in their respective matches in a row, the same column matches different heights, the distance is defined between the two matches are: ( a i b i ) 2 Σ (ai-bi) ^ 2
where ai represents the height of the first matches in the i-th row matches, bi represents the height of the second row matches the first number of matches iii.
Column matches the position of each adjacent two of the matches can be exchanged by exchanging you that the minimum distance between the two matches. Will get this minimum distance, the exchange requires a minimum number of times? If this number is too big, the minimum switching frequency and outputs the result to the modulo 99,999,997.
Input Output Format
Input Format:
total three lines, the first line contains an integer n, the number of matches in each box.
The second row there are n integers, each between two integers separated by a space, the first column height matches.
The third row there are n integers, each between two integers separated by a space, the second column height matches.
Output format:
an integer, represents the minimum switching frequency and the results of the modulo 99,999,997.

Sample Input Output
Input Sample # 1:
. 4
2 1. 3. 4
. 3. 4 2 1

Output Sample # 1:
1

Input Sample # 2:
. 4
. 1 2. 4. 3
. 1 2. 4. 7

Output Sample # 2:
2

DESCRIPTION
100% of the data, 1≤n≤100,000,0≤ height matches ≤maxlongint

Explanation: first determine which of two pairs corresponds to the number, apparently sorted, from small to large correspondence, correspond to complete. Re-encoding the first number of lines corresponding to the number of positions, so that the minimum number of programming becomes a problem from an ordered sequence of one sequence. Directly on seeking merge sort in reverse order number

#include<iostream>
#include<algorithm>
#define N 100005
#define mod 99999997
using namespace std;
struct node{
    int v;
    int id;
};
node A[N],B[N];
int a[N]={0},b[N]={0};
int n=0;
bool cmp1(node &a,node &b){
    return a.v<b.v;
}
bool cmp2(node &a,node &b){
    return a.id<b.id;
}
long long merge(int *a,int l,int r){
    int i=l,mid=(l+r)>>1,j=mid+1;
    int index=l;
    long long sum=0;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j]){
            b[index++]=a[i++];
        }else{
            b[index++]=a[j++];
            sum+=(long long)mid-i+1;
            sum%=mod;
        }
    }
    while(i<=mid) b[index++]=a[i++];
    while(j<=r) b[index++]=a[j++];
    for(int i=l;i<=r;i++) a[i]=b[i];
    return sum%mod;
}
long long Qsort(int *a,int l,int r){
    if(l>=r) return 0;
    long long ret=0,mid=(l+r)>>1;
    ret+=Qsort(a,l,mid);
    ret%=mod;
    ret+=Qsort(a,mid+1,r);
    ret%=mod;
    ret+=merge(a,l,r);
    ret%=mod;
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>A[i].v;A[i].id=i;
    }
    for(int i=1;i<=n;i++){
        cin>>B[i].v;B[i].id=i;
    }
    sort(A+1,A+1+n,cmp1);
    sort(B+1,B+1+n,cmp1);
    for(int i=1;i<=n;i++) A[i].v=B[i].id;
    sort(A+1,A+1+n,cmp2);
    for(int i=1;i<=n;i++) a[i]=A[i].v;
    cout<<Qsort(a,1,n)%mod<<endl;
    return 0;
}



Guess you like

Origin blog.csdn.net/mkopvec/article/details/92588058