hdu1425 sort 解题报告

 Problem 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
解题思路: 刚看到这个题,数据那么大,就觉得用排序方法应该是会超时的,所
以就选了快速排序试一试的心 态,最终是没有达到效果,后来网络里搜一下,知道了
另一种对数据的处理方法,直接把数据弄到数组里,就 相当于已经排序了,这是一种
不普通的解法,还是异于寻常的。速度还是非常的快。
我的快速排序的代码如下:
Code:
  1. #include<iostream>  
  2. using namespace std;  
  3. int a[1000001]={0};  
  4. int n,m;  
  5.   
  6. int Partition(int a[],int low,int high)      //进行一趟的排序  
  7. {  
  8.     int pre=a[low];  
  9.     while(low<high)  
  10.     {  
  11.         while(low<high&&a[high]>=pre)  
  12.             high--;  
  13.         int temp;  
  14.         temp=a[low];  
  15.         a[low]=a[high];  
  16.         a[high]=temp;  
  17.         while(low<high&&a[low]<=pre)  
  18.             low++;  
  19.         temp=a[low];  
  20.         a[low]=a[high];  
  21.         a[high]=temp;  
  22.     }  
  23.     return low;  
  24. }  
  25.   
  26. void QSort(int a[],int low,int high)        //递归  
  27. {  
  28.       
  29.     if(low<high)  
  30.     {  
  31.         int pivotloc=Partition(a,low,high);  
  32.         QSort(a,low,pivotloc-1);  
  33.         QSort(a,pivotloc+1,high);  
  34.     }  
  35. }  
  36.   
  37. void QuickSort(int a[],int j)  
  38. {  
  39.     QSort(a,1,j-1);  
  40. }  
  41.   
  42. int main()  
  43. {  
  44.     int temp;  
  45.       
  46.     while(cin>>n>>m)  
  47.     {  
  48.         int j=1;  
  49.         for(int i=1;i<=n;i++)  
  50.         {  
  51.             cin>>temp;  
  52.             if(temp>=m)  
  53.             {  
  54.                 a[j]=temp;  
  55.                 j++;  
  56.             }  
  57.         }  
  58.         QuickSort(a,j);  
  59.         for(i=j-1;i>0;i--)  
  60.             cout<<a[i]<<endl;  
  61.     }  
  62.     return 0;  
  63. }  
推荐AC的方法(代码如下):
Code:
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     unsigned n, m;  
  5.     int x;  
  6.     char a[1000001] = {0};  
  7.     while (scanf("%d%d", &n, &m) != EOF)  
  8.     {  
  9.         while (n--)  
  10.         {  
  11.             scanf("%d", &x);  
  12.             a[500000+x]++;  
  13.         }  
  14.         for (x = 1000000; m; x--)  
  15.         {  
  16.             if (a[x])  
  17.             {  
  18.                 printf("%d", x-500000);  
  19.                 if (--m)  
  20.                     putchar(32);  
  21.                 else  
  22.                     putchar(10);  
  23.                 a[x]--;  
  24.             }  
  25.         }  
  26.     }  
  27.     return 0;  
  28. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值