反转计数给出大输入的错误
给定代码是用于计算给定数组中的反转,或者我们可以说要计算数组中要进行的更改数量,以便将其分类。 代码对低于10 ** 5的输入工作正常,但是在此之前,我无法解决错误。 我还使用需要长的长数据类型,以便它可以处理大型输入。
long long merge(long long arr[], int low, int mid, int high){
long long count = 0;
int l1 = mid-low+1;
int l2 = high - mid;
long long arr1[l1];
long long arr2[l2];
for(int i=0;i<l1;i++){
arr1[i] = arr[low+i];
}
for(int i=0;i<l2;i++){
arr2[i] = arr[mid+1+i];
}
int i=0, j=0, k=low;
while(i!=l1 and j!=l2){
if(arr1[i]<arr2[j]){
arr[k++] = arr1[i++];
}
else{
count+=l1-i;
arr[k++] = arr2[j++];
}
}
while(i<l1)
arr[k++] = arr1[i++];
while(j<l2)
arr[k++] = arr2[j++];
return count;
}
long long mergesort(long long arr[], int low, int high){
long long count = 0;
if(low<high){
int mid = (low+high)/2;
count+=mergesort(arr, low, mid);
count+=mergesort(arr, mid+1, high);
count+=merge(arr, low, mid, high);
}
return count;
}
long long solve(long long *arr, int n){
if(n==1) return 0;
return mergesort(arr,0, n);
}
The given code is for counting the inversions in the given array or we can say to count the number of changes to be made in an array so that it becomes sorted.
Code is working fine for the inputs lower than 10**5 but above that it is giving error and I am unable to solve the error.
I also use long long data type where it is required so that it can handle large inputs.
long long merge(long long arr[], int low, int mid, int high){
long long count = 0;
int l1 = mid-low+1;
int l2 = high - mid;
long long arr1[l1];
long long arr2[l2];
for(int i=0;i<l1;i++){
arr1[i] = arr[low+i];
}
for(int i=0;i<l2;i++){
arr2[i] = arr[mid+1+i];
}
int i=0, j=0, k=low;
while(i!=l1 and j!=l2){
if(arr1[i]<arr2[j]){
arr[k++] = arr1[i++];
}
else{
count+=l1-i;
arr[k++] = arr2[j++];
}
}
while(i<l1)
arr[k++] = arr1[i++];
while(j<l2)
arr[k++] = arr2[j++];
return count;
}
long long mergesort(long long arr[], int low, int high){
long long count = 0;
if(low<high){
int mid = (low+high)/2;
count+=mergesort(arr, low, mid);
count+=mergesort(arr, mid+1, high);
count+=merge(arr, low, mid, high);
}
return count;
}
long long solve(long long *arr, int n){
if(n==1) return 0;
return mergesort(arr,0, n);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
合并函数正在使用本地堆栈空间用于可变长度阵列arr1 [],arr2 [],并用尽了大数组的堆栈空间。将代码合并为:
但是,合并被称为大型数组,进行大量新闻和删除。最好是进行一次时间分配并将其复制到第二个数组,然后根据递归级别进行合并方向,如Wiki示例所示:
https://en.wikipedia.org/wiki/wiki/merge_sort#top-down_implementation
The merge function is using local stack space for the variable length arrays arr1[], arr2[], and runs out of stack space for large arrays. Change the code in merge to:
However merge is called a lot of times for a large array, doing a lot of news and deletes. It would be better to do a one time allocation and copy to a second array, then alternate direction of merge based on level of recursion, as shown in the wiki example:
https://en.wikipedia.org/wiki/Merge_sort#Top-down_implementation
在这里,
您可以在堆栈上创建潜在的大型物品。在其他地方,您使用堆。您应该坚持使用堆
here
you arre creating potentially large items on the stack. In other places you use the heap. You should stick to using the heap