1#include <iostream>2#include <stdio.h>3#include <cstring>4#include <vector>5 using namespacestd;6 inthigh[100010];7 inttemp[100010];8 intangry[100010];9 intnum;Ten voidMergeSort (intFirstintMidintLast ) One { A intLeft1=first,right1=mid,left2=mid+1, right2=Last ; - intI=0; - while(left1<=right1&&left2<=right2) the { - if(high[left1]<=high[left2]) - { -temp[i++]=high[left1++]; + } - Else + { Atemp[i++]=high[left2++]; atangry[left2]+=right1-left1+1; -num+=right1-left1+1; - for(intJ=LEFT1; j<=right1; J + +) - { -angry[j]++; - } in - } to } + if(left1>right1) - { the while(left2<=right2) * { $temp[i++]=high[left2++];Panax Notoginseng } - } the Else if(left2>right2) + { A while(left1<=right1) the { +temp[i++]=high[left1++]; - } $ } $ for(intI=0; i<last-first+1; i++) - { -high[first+i]=Temp[i]; the } - Wuyi } the voidSort (intFirstintLast ) - { Wu if(first<Last ) - { About intMid= (first+last)/2; $ Sort (first,mid); -Sort (mid+1, last); - mergesort (first,mid,last); - } A } + intMain () the { - intN; $ while(SCANF ("%d", &n)! =EOF) the { thenum=0; the for(intI=0; i<n; i++) thescanf"%d",&high[i]); -Sort (0, N-1); in for(intI=0; i<n; i++) the { theprintf"%d", Angry[i]); About } thecout<<Endl; thecout<<num<<Endl; the } + - return 0; the}View Code
Merge sort to find the inverse number pair.