Ultra-quiksort (p2299)

这个题,先是数组开小了,然后是输出结果用int存不下。


时候超过了1000MS,时间上比较长啊。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>

using namespace std;

#define N 1111111

int n;
struct my
{
	int id;
	long long  v;
	
}
go[N];
int hi[N];

int t[N];

bool cmp1(my a,my b)
{
	return a.v<b.v;
}

bool cmp2(my a,my b)
{
	return a.id>b.id;
}

void update(int k)
{
	k+=2;
	while (k<N)
	{
		t[k]++;
		k+=k&-k;
	}
}

int get(int k)
{
	k+=2;
	int sum=0;
	while (k)
	{
		sum+=t[k];
		k-=k&-k;
	}
	return sum;

}

int main()
{
	freopen("in.txt","r",stdin);

	int i,j,k;

	while (cin>>n,n)
	{
		for (i=0;i<n;i++)
		{
			scanf("%lld",&go[i].v);
	
			go[i].id=i;
		}
		sort(go,go+n,cmp1);

		k=1;
		for (i=0;i<n;i++)
		{
			go[i].v=k++;
		}

		sort(go,go+n,cmp2);


		memset(t,0,sizeof(t));

		long long  ans;
		ans=0;

		for (i=0;i<n;i++)
		{
			ans+=get(go[i].v);
			update(go[i].v);
		}

		cout<<ans<<endl;
		

	}
	return 0;
}


Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 27087 Accepted: 9707

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,这段代码似乎没有完整的实现,其中有一些空缺的部分需要您填充完整。不过,我可以根据这段代码的大致逻辑给您解释一下快速排序的基本实现过程。 快速排序的基本思路是:选定一个基准数,将数组中小于基准数的元素移到基准数左边,将大于基准数的元素移到基准数右边。然后递归处理基准数左右两边的子数组,直到子数组长度为1或者0,排序完成。 根据上述思路,我们可以将代码中的空缺部分填充完整。下面是我对代码的修改和补充: ```c #include <stdio.h> #define MAX 100 void quicksort(int a[], int low, int high) { int i = low; int j = high; int temp = a[i]; if (low < high) { // 子数组长度大于1时执行快速排序 while (i < j) { while ((a[j] >= temp) && (i < j)) { j--; } a[i] = a[j]; while ((a[i] <= temp) && (i < j)) { i++; } a[j] = a[i]; } a[i] = temp; quicksort(a, low, i - 1); // 递归排序左子数组 quicksort(a, j + 1, high); // 递归排序右子数组 } } int main() { int arry[MAX]; int i, n; printf("请输入要排序的数据的个数:\n"); scanf("%d", &n); printf("\n"); printf("请输入要排序的数据:\n"); for (i = 0; i < n; i++) { scanf("%d", &arry[i]); } quicksort(arry, 0, n - 1); // 调用快速排序函数对数组进行排序 for (i = 0; i < n; i++) { printf("%d ", arry[i]); // 输出排序后的结果 } printf("\n"); return 0; } ``` 以上就是我对代码的修改和补充,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值