#include <bits/stdc++.h>
#include "teatr.h"
using namespace std;
int tab[10000005];
int tab2[10000005];
long long int sum;
long long int sumt;
void pom(int low, int mid, int high){
int a,b,c;
for(int i=low; i<=high; i++){
tab2[i]=tab[i];
}
a=low; b=mid+1; c=low;
sumt=0;
while(a<=mid && b<=high){
if(tab2[a]>=tab2[b]){
tab[c++]=tab2[a++];
}
else{
tab[c++]=tab2[b++];
sum+=mid-a+1;
}
}
while(a<=mid){
tab[c++]=tab2[a++];
}
}
void sortowanie(int low, int high){
if (low<high) {
int mid=(low+high)/2;
sortowanie(low,mid);
sortowanie(mid+1,high);
pom(low,mid,high);
}
}
int main(){
int n=GetN();
sum=0;
for(int j=0; j<n; j++){
tab[j]=GetElement(n-j-1);
}
sortowanie(0,n-1);
cout<<sum;
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 | #include <bits/stdc++.h> #include "teatr.h" using namespace std; int tab[10000005]; int tab2[10000005]; long long int sum; long long int sumt; void pom(int low, int mid, int high){ int a,b,c; for(int i=low; i<=high; i++){ tab2[i]=tab[i]; } a=low; b=mid+1; c=low; sumt=0; while(a<=mid && b<=high){ if(tab2[a]>=tab2[b]){ tab[c++]=tab2[a++]; } else{ tab[c++]=tab2[b++]; sum+=mid-a+1; } } while(a<=mid){ tab[c++]=tab2[a++]; } } void sortowanie(int low, int high){ if (low<high) { int mid=(low+high)/2; sortowanie(low,mid); sortowanie(mid+1,high); pom(low,mid,high); } } int main(){ int n=GetN(); sum=0; for(int j=0; j<n; j++){ tab[j]=GetElement(n-j-1); } sortowanie(0,n-1); cout<<sum; return 0; } |
English