#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include "message.h"
#include "teatr.h"
#define MAX 100000100
using namespace std;
int tab[10];
int buf[MAX];
unsigned long long ans = 0;
int temp[MAX];
unsigned long long merge(int arr[],int temp[],int left,int mid,int right)
{
int i=left;
int j=mid;
int k=left;
unsigned long long int invcount=0;
while((i<=mid-1) && (j<=right))
{
if(arr[i]<=arr[j])
temp[k++]=arr[i++];
else
{
temp[k++]=arr[j++];
invcount+=(mid-i);
}
}
while(i<=mid-1)
temp[k++]=arr[i++];
while(j<=right)
temp[k++]=arr[j++];
for(int i=left;i<=right;i++)
arr[i]=temp[i];
return invcount;
}
unsigned long long int mergesort(int arr[],int temp[],int left,int right)
{
unsigned long long int invcount=0;
if(right>left){
int mid=(left+right)/2;
invcount=mergesort(arr,temp,left,mid);
invcount+=mergesort(arr,temp,mid+1,right);
invcount+=merge(arr,temp,left,mid+1,right);
}
return invcount;
}
int main() {
if(MyNodeId() != 0) {
return 0;
}
int n = GetN();
unsigned long long res2 = 0;
int mx = 0;
for(int i = 0; i < n; ++i) {
int el = GetElement(i);
if(el > mx) {
mx = el;
}
buf[i] = el;
}
if(mx <= 5) {
for(int i = 0; i < n; ++i) {
++tab[buf[i]];
for(int j = 0; j < 10; ++j) {
if(j > buf[i]) {
res2 += tab[j];
}
}
}
cout << res2 << endl;
} else {
cout << mergesort(buf, temp, 0, n-1) << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include "message.h" #include "teatr.h" #define MAX 100000100 using namespace std; int tab[10]; int buf[MAX]; unsigned long long ans = 0; int temp[MAX]; unsigned long long merge(int arr[],int temp[],int left,int mid,int right) { int i=left; int j=mid; int k=left; unsigned long long int invcount=0; while((i<=mid-1) && (j<=right)) { if(arr[i]<=arr[j]) temp[k++]=arr[i++]; else { temp[k++]=arr[j++]; invcount+=(mid-i); } } while(i<=mid-1) temp[k++]=arr[i++]; while(j<=right) temp[k++]=arr[j++]; for(int i=left;i<=right;i++) arr[i]=temp[i]; return invcount; } unsigned long long int mergesort(int arr[],int temp[],int left,int right) { unsigned long long int invcount=0; if(right>left){ int mid=(left+right)/2; invcount=mergesort(arr,temp,left,mid); invcount+=mergesort(arr,temp,mid+1,right); invcount+=merge(arr,temp,left,mid+1,right); } return invcount; } int main() { if(MyNodeId() != 0) { return 0; } int n = GetN(); unsigned long long res2 = 0; int mx = 0; for(int i = 0; i < n; ++i) { int el = GetElement(i); if(el > mx) { mx = el; } buf[i] = el; } if(mx <= 5) { for(int i = 0; i < n; ++i) { ++tab[buf[i]]; for(int j = 0; j < 10; ++j) { if(j > buf[i]) { res2 += tab[j]; } } } cout << res2 << endl; } else { cout << mergesort(buf, temp, 0, n-1) << endl; } return 0; } |
English