SZU 2015 Winter Training Day#1

A - sort

Time Limit:1000MS     MemoryLimit:32768KB     64bit IO Format:%I64d& %I64u

Submit Status

Description

给你n个整数,请按从大到小的顺序输出其中前m大的数。 

 

Input

每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。 

 

Output

对每组测试数据按从大到小的顺序输出前m大的数。 

 

Sample Input

 5 3

3 -35 92 213-644

 

Sample Output

 213 92 3

Hint

Hint

请用VC/VC++提交


 

1、快速排序
#include <iostream>
using namespace std;

int a[1000000];

void qsort(int,int);
void pr(int);

int main()
{
	int n,m,i;

	while (scanf("%d%d",&n,&m)!= EOF)
	{
		memset(a,0,sizeof(a));
		for (i=0;i<n;i++)
			scanf("%d",&a[i]);
		qsort(0,n-1);
		pr(m);
	}

	return 0;
}

void qsort(int l ,int r)
{
	int x,y,i,j;
	
	x=a[(l+r)/2];
	i=l;
	j=r;

	while (i<=j)
	{
 		while (a[i]>x) i++;
		while (a[j]<x) j--;
		if (i<=j)
		{
			y=a[i];
			a[i]=a[j];
			a[j]=y;
			i++;
			j--;
		}
	}

	if (l<j) qsort(l,j);
	if (i<r) qsort(i,r);
}

void pr(int m)
{
	int i;
	for (i=0;i<m-1;i++)
		printf("%d ",a[i]);
	printf("%d\n",a[i]);
}
 
2、归并排序
#include <iostream>

#define rep(i,l,r) for (i=l;i<r;i++)
using namespace std;

int a[1000000]={0},b[1000000]={0};

void merge(int,int,int);
void mergesort(int,int);
void pr(int [],int);

int main()
{
	int i,n,m;

	//memset(a,0,sizeof(a));
	//memset(b,0,sizeof(b));

	while (scanf("%d%d",&n,&m)!=EOF)
	{
		rep(i,0,n)
			scanf("%d",&a[i]);
	
		mergesort(0,n-1);

		pr(a,m);
	}

	return 0;
}

void merge(int l,int k,int r)
{
	//memset(b,0,sizeof(b));

	int i=l,j=k+1,p=l;
	while (i<=k && j<=r)
	{
		if (a[i]>a[j])
			b[p++]=a[i++];
		else 
			b[p++]=a[j++];
	}

	while (i<=k) b[p++]=a[i++];
	while (j<=r) b[p++]=a[j++];

	for (i=l;i<=r;i++)
		a[i]=b[i];
}

void mergesort(int l,int r)
{
	int k;

	k=(l+r)/2;
	if (r<=l) ;
	else
	{
		mergesort(l,k);
		mergesort(k+1,r);
		merge(l,k,r);
	}
}


void pr(int ar[],int m)
{
	int i;
	rep(i,0,m-1)
		printf("%d ",ar[i]);
	printf("%d\n",ar[i]);
}
 
3、堆排序
#include <iostream>

#define rep(i,l,r) for (i=l;i<=r;i++)

using namespace std;

int a[1000000];

void HeapUpAdjust(int,int);
void HeapDownAdjust(int,int);
void HeapSort(int);
void Pr(int l,int r);

int main()
{
	int i,n,m;

	memset(a,0,sizeof(a));

	while (scanf("%d%d",&n,&m)!=EOF)
	{
		rep(i,1,n)
			scanf("%d",&a[i]);

		HeapUpAdjust(1,n);

		HeapSort(n);

		Pr(n-m+1,n);
	}

	return 0;
}

void HeapSort(int n)
{
	if (n==1) return;

	swap(a[1],a[n]);
	HeapDownAdjust(1,n-1);
	HeapSort(n-1);
}

void HeapUpAdjust(int l,int r)
{
	int i;

	for (i=r;i>l;i--)
		if (a[i]>a[i/2])
		{
			swap(a[i],a[i/2]);
			HeapDownAdjust(i,r);
		}
}

void HeapDownAdjust(int k,int n)
{
	int max;

	max=a[k];
	if (a[k]<a[k*2] && k*2<=n) max=a[k*2];
	if (max<a[(k*2)+1] && k*2<n) max=a[k*2+1];
	if (!(max-a[k]))
		return;
	else if (!(max-a[k*2]))
	{
		swap(a[k],a[k*2]);
		HeapDownAdjust(k*2,n);
	}
	else 
	{
		swap(a[k],a[k*2+1]);
		HeapDownAdjust(k*2+1,n);
	}
}

void Pr(int l,int r)
{
	int i;

	for (i=r;i>l;i--)
		printf("%d ",a[i]);
	printf("%d\n",a[i]);
}
 

B - Ultra-QuickSort

Time Limit:7000MS     MemoryLimit:65536KB     64bit IO Format:%I64d& %I64u

Submit Status

Description

In this problem, you have to analyze a particular sorting algorithm. Thealgorithm processes a sequence of n distinct integers by swapping two adjacentsequence elements until the sequence is sorted in ascending order. For theinput 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 needsto perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with aline that contains a single integer n < 500,000 -- the length of the inputsequence. 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 terminatedby a sequence of length n = 0. This sequence must not be processed.

Output

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

Sample Input

5

9

1

0

5

4

3

1

2

3

0

Sample Output

6

0

 

程序:
#include <iostream>
using namespace std;

int a[1000000]={0},b[1000000]={0};

__int64 merge(int,int,int);
__int64 mergesort(int,int);

int main()
{
	int n,i;

	memset(a,0,sizeof(a));

	while (1)
	{
		scanf("%d",&n);
		if (!n) break;
		for (i=0;i<n;i++)
			scanf("%d",&a[i]);
		printf("%I64d\n",mergesort(0,n-1));
	}
	return 0;
}

__int64 merge(int l,int k,int r)
{
	//memset(b,0,sizeof(b));

	int i=l,j=k+1,p=l;
	__int64 total=0;

	while (i<=k && j<=r)
	{
		if (a[i]>a[j])
		{
			b[p++]=a[j++];
			total+=k-i+1;
		}
		else 
			b[p++]=a[i++];
	}

	while (i<=k) b[p++]=a[i++];
	while (j<=r) b[p++]=a[j++];

	for (i=l;i<=r;i++)
		a[i]=b[i];

	return total;
}

__int64 mergesort(int l,int r)
{
	int k;
	__int64 total=0;

	k=(l+r)/2;
	if (l<r)
	{
		total+=mergesort(l,k);
		total+=mergesort(k+1,r);
		total+=merge(l,k,r);
	}
	return total;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值