#include <iostream>
#include <cstring>
using namespace std;
// int tmp1[260000] = {0};
// int tmp2[260000] = {0};
int t[500002] = {0};
int array[500002] = {0};
long long mergeSort(int * array, int begin, int end, int MAXlength) {
// cout<<"begin "<<begin<<" end "<<end<<endl;
// for (int i = 0; i< MAXlength; i++) {
// cout<<" "<<array[i];
// }
// cout<<endl;
int length = end - begin + 1;
if (begin > end) {
return 0;
}
if (begin == end) {
return 0;
}
if (begin + 1 == end) {
if (array[begin] > array[end]) {
int tmp = array[begin];
array[begin] = array[end];
array[end] = tmp;
return 1;
}
return 0;
}
long long swapSum = 0;
int middle = (begin + end)/2;
swapSum += mergeSort(array, begin, middle, MAXlength);
swapSum += mergeSort(array, middle + 1, end, MAXlength);
// cout<<"swapSum "<<swapSum<<endl;
// for (int i = begin; i<= end; i++) {
// cout<<"before merge"<<array[i]<<endl;
// }
int leftLength = middle - begin + 1;
int rightLength = end - middle - 1 + 1;
// for (int i = 0; i < leftLength; i++) {
// tmp1[i] = array[begin+i];
// // cout<<"tmp1 "<<tmp1[i]<<endl;
// }
// for (int i = 0; i < rightLength; i++) {
// tmp2[i] = array[middle + 1 + i];
// // cout<<"tmp2 "<<tmp2[i]<<endl;
// }
int rightbegin = middle + 1;
int i = begin;
int j = rightbegin;
int tmpIndex = 0;
while(1) {
if (i > middle) {
break;
}
if (j > end) {
break;
}
if (array[i] > array[j]) {
t[tmpIndex++] = array[j++];
} else {
t[tmpIndex++] = array[i++];
swapSum += j -rightbegin;
// cout<<"swap += "<<j+1<<endl;
}
}
if (i > middle) {
// cout<<"i MAX"<<j<<" "<<rightLength<<endl;
for (; j <= end; j++) {
t[tmpIndex++] = array[j];
// cout<<tmp2[j]<<endl;
}
} else if (j > end) {
// cout<<"j MAX"<<i<<" "<<leftLength<<endl;
for (; i <= middle; i++) {
t[tmpIndex++] = array[i];
// cout<<tmp1[i]<<endl;
swapSum += rightLength;
// cout<<"swap += "<<rightLength<<endl;
}
}
int k = 0;
for (int i = begin; i <= end; i++) {
array[i] = t[k++];
}
return swapSum;
}
int main() {
while(1) {
int num = 0;
cin>>num;
if(!num) {
return 0;
}
// long long * array = new long long[num];
for(int i = 0; i < num; i++) {
cin>>array[i];
}
long long swapNum = mergeSort(array, 0, num-1, num);
// for (int i = 0; i< num; i++) {
// cout<<" "<<array[i];
// }
// cout<<endl;
cout<<swapNum<<endl;
// delete[] array;
// array = NULL;
}
}
#include <cstring>
using namespace std;
// int tmp1[260000] = {0};
// int tmp2[260000] = {0};
int t[500002] = {0};
int array[500002] = {0};
long long mergeSort(int * array, int begin, int end, int MAXlength) {
// cout<<"begin "<<begin<<" end "<<end<<endl;
// for (int i = 0; i< MAXlength; i++) {
// cout<<" "<<array[i];
// }
// cout<<endl;
int length = end - begin + 1;
if (begin > end) {
return 0;
}
if (begin == end) {
return 0;
}
if (begin + 1 == end) {
if (array[begin] > array[end]) {
int tmp = array[begin];
array[begin] = array[end];
array[end] = tmp;
return 1;
}
return 0;
}
long long swapSum = 0;
int middle = (begin + end)/2;
swapSum += mergeSort(array, begin, middle, MAXlength);
swapSum += mergeSort(array, middle + 1, end, MAXlength);
// cout<<"swapSum "<<swapSum<<endl;
// for (int i = begin; i<= end; i++) {
// cout<<"before merge"<<array[i]<<endl;
// }
int leftLength = middle - begin + 1;
int rightLength = end - middle - 1 + 1;
// for (int i = 0; i < leftLength; i++) {
// tmp1[i] = array[begin+i];
// // cout<<"tmp1 "<<tmp1[i]<<endl;
// }
// for (int i = 0; i < rightLength; i++) {
// tmp2[i] = array[middle + 1 + i];
// // cout<<"tmp2 "<<tmp2[i]<<endl;
// }
int rightbegin = middle + 1;
int i = begin;
int j = rightbegin;
int tmpIndex = 0;
while(1) {
if (i > middle) {
break;
}
if (j > end) {
break;
}
if (array[i] > array[j]) {
t[tmpIndex++] = array[j++];
} else {
t[tmpIndex++] = array[i++];
swapSum += j -rightbegin;
// cout<<"swap += "<<j+1<<endl;
}
}
if (i > middle) {
// cout<<"i MAX"<<j<<" "<<rightLength<<endl;
for (; j <= end; j++) {
t[tmpIndex++] = array[j];
// cout<<tmp2[j]<<endl;
}
} else if (j > end) {
// cout<<"j MAX"<<i<<" "<<leftLength<<endl;
for (; i <= middle; i++) {
t[tmpIndex++] = array[i];
// cout<<tmp1[i]<<endl;
swapSum += rightLength;
// cout<<"swap += "<<rightLength<<endl;
}
}
int k = 0;
for (int i = begin; i <= end; i++) {
array[i] = t[k++];
}
return swapSum;
}
int main() {
while(1) {
int num = 0;
cin>>num;
if(!num) {
return 0;
}
// long long * array = new long long[num];
for(int i = 0; i < num; i++) {
cin>>array[i];
}
long long swapNum = mergeSort(array, 0, num-1, num);
// for (int i = 0; i< num; i++) {
// cout<<" "<<array[i];
// }
// cout<<endl;
cout<<swapNum<<endl;
// delete[] array;
// array = NULL;
}
}
C++ 1230MS
归并排序给过的, 时间惨不忍睹..., 关键有哥们也是归并就144ms(copy 代码试了下, 677ms C++, 不过也不错了)..., 一开始以为是数组拷贝的问题, 后来换成一样的方式,还不行.
先贴着, 好好研究:
#include<stdio.h> #include<string.h> int a[500005],t[500005]; __int64 merge_sort(int l,int r,int len) { int i,temp,j; __int64 ans=0; if(len==0) return 0; temp=(l+r)/2; ans+=merge_sort(l,temp,temp-l); ans+=merge_sort(temp+1,r,r-temp-1); int p=l,q=temp+1; j=l; for(i=0;i<=len;i++) { if(q>r||(p<=temp&&a[p]<a[q])) t[j++]=a[p++]; else { ans+=temp-p+1; t[j++]=a[q++]; } } for(;l<=r;l++) a[l]=t[l]; return ans; } inline void scan(int &n) { char c; //while(c=getchar(),c<'0'||c>'9'); n=0; while(c=getchar(),c<='9'&&c>='0') n=n*10+c-'0'; } int main() { int n,i; __int64 ans; while(scanf("%d",&n),n) { getchar(); for(i=0;i<n;i++) scan(a[i]); ans=merge_sort(0,n-1,n-1); printf("%I64d\n",ans); } }
题目不难, 就是求逆序数, 不过一开始没领会,直观的想到冒泡搞,果然TLE。
后来归并时又用错了memcpy(不能用在int数组上), 卡了会, 其他就没啥难点了.
归并时, left right两个数组, 在把left[i] 填到合并数组时, 累加当前right已经有多少个进去(逆序了, left > right), 就是最后的结果.
还有就是交换次数用long long。
看后面还有别的解发,树状数组, 以后搞那一块的时候试试.