P1908 reverse order - (cdq partition)

https://www.luogu.org/problem/P1908

Follow merge sort of thought seeking reverse right.

Pit 1: Results of the explosion int type, need longlong

Pit 2: relative to merge sort, when comparing more than an equal sign

Solutions merge sort illustrates this question, for example, number 6,

36,87,99, the range interval is l to the left mid, denoted by the subscript t1

1,2,50, the right interval in the range of mid + 1 to r, denoted by the subscript t2

Divided into two piles, piles sorted, to merge. In this case l = 1, mid = 3, t1 = 1; mid + 1 = 4, r = 6, t2 = 4;

Comparison 1 and 36, selected from the group 1, the left side are not yet ordered number 1 and constitute reverse order, 3, 36,87,99, mid-t1 + 1 = 3;

Comparison of the number of 36 and 2, selected from the group 2, the left side are not yet sorted reverse order and configuration 2, 3, 36,87,99, mid-t1 + 1 = 3;

Comparative 36 and 50, 36 is selected, there is no reverse of the configuration;

Comparison of the number 87 and 50, 50 is selected, the left side are not yet sorted and 50 constitute a reverse order, two, 87,99, mid-t1 + 1 = 2;

Right interval has been drained, the number of direct elections left the interval, did not constitute reverse right.

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<ctime>
#define ll long long
#define inf 0x3f3f3f3f
const double pi=3.1415926;
using namespace std;

const  int Maxx = 500 005 ;
 int A [Maxx]; // / sorted array 
int B [Maxx]; // / original array 
int n-;
ll ans=0;

void The cdq ( int L, int R & lt, int X []) // / closed interval around, x array as a parameter passed 
{
     IF (L == R & lt)
         return ; // outlet 
    int MID = (L + R & lt) / 2 ;
    cdq(l,mid,x);
    The cdq (MID + 1'd , R & lt, X);
     int T1 = L, + T2 = MID. 1; // / left pointers 
    for ( int I = L; I <= R & lt; I ++ )
    {
        // / (the right section of the current value of the current value of the left subranges <= not exceeded and the left hand maximum to the left) or right row has been taken over to the left 
        IF ((X [T1] <= X [T2] && T1 <= MID) || T2> R & lt) // is equal to the number of the long pit 
            a [I] = X [T1 ++ ];
         the else  // / take it does not take left and right by the number of cycles for guaranteed 
        {
            a[i]=x[t2++];
            ANS + = (LL) (+ MID-T1. 1); // / left interval if there is left, that is, under the current and t2 constitutes the subject of the number of reverse 
        }
    }
    for ( int I = L; I <= R & lt; I ++) // / b to be exchanged array 
        X [I] = A [I];
}


int main()///P1908
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    cdq(1,n,b);
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/shoulinniao/p/11618890.html