zoukankan      html  css  js  c++  java
  • hdu 1425:sort(排序,经典题。快排模板)

    sort

    Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 3   Accepted Submission(s) : 2

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    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
    

    Hint

    Hint
    请用VC/VC++提交

    Author

    LL

    Source

    ACM暑期集训队练习赛(三)
     
      排序算法,综合练习。
      这道题可以作为排序算法的综合练习,题意很简单,没有那么多弯弯绕绕,对n个数进行排序,然后输出前m个大的数。数据规模很大,冒泡会超时。有疑惑的话可以用下方冒泡排序代码测试一下。
     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 int a[1000001];
     5 int main()
     6 {
     7     int n,m;
     8     while(cin>>n>>m){
     9         int i,j;
    10         for(i=1;i<=n;i++)
    11             scanf("%d",&a[i]);
    12         for(i=1;i<=n-1;i++)
    13             for(j=1;j<=n-i;j++)
    14                 if(a[j]>a[j+1]){
    15                     int t;
    16                     t=a[j];a[j]=a[j+1];a[j+1]=t;
    17                 }
    18         for(i=n;i>=n-m+1;i--){
    19             if(i==n-m+1)
    20                 cout<<a[i]<<endl;
    21             else
    22                 cout<<a[i]<<' ';
    23         }
    24     }
    25     return 0;
    26 }
    View Code

      惭愧的是,为了省事,这道题我直接套用了<algorithm>中的sort()函数,从网上得知该函数使用的是内省排序,时间复杂度是O(nlogn),应付这道题是够了,时间用了500+MS。当然你也可以采用快排,时间复杂度也是O(nlogn),对付这道题也没问题。

      注意:输出前m个数的时候,最后一个数后面不能带空格' ',否则提交会格式错误。

      有时间会重新用这道题练习一下,低效率的算法通过不了这道题。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int a[1000001];
     6 int main()
     7 {
     8     int n,m;
     9     while(cin>>n>>m){
    10         int i;
    11         for(i=1;i<=n;i++)    //输入
    12             scanf("%d",&a[i]);
    13         sort(a+1,a+n+1);    //从小到大排序
    14         for(i=n;i>=n-m+1;i--){    //输出前m个大的数
    15             if(i==n-m+1)
    16                 cout<<a[i]<<endl;
    17             else
    18                 cout<<a[i]<<' ';
    19         }
    20     }
    21     return 0;
    22 }
    Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
    10133960 2014-02-20 15:49:42 Accepted 1425 593MS 2284K 343 B C++ freecode

     

      正好看到数据结构内排序这一部分,想起了这道题,拿它来练习快速排序正好。

      注意:输入的时候尽量用scanf(),用cin会超时。

      快排模板

     1 void quicksort(int a[],int s,int t)    //对a[]的第s个到第t个元素进行从小到大的排序
     2 {
     3     int i=s,j=t;
     4     int tmp = a[s];
     5     if(s<t){    //区间内元素剩0个或者1个的时候停止
     6         while(i<j){
     7             while(i<j && a[j]>=tmp)
     8                 j--;
     9             a[i] = a[j];
    10             while(i<j && a[i]<=tmp)
    11                 i++;
    12             a[j] = a[i];
    13         } 
    14         a[i] = tmp;
    15         quicksort(a,s,i-1);    //对左区间递归排序 
    16         quicksort(a,i+1,t);    //对右区间递归排序 
    17     }
    18 }

      本题代码

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 int a[1000001];
     5 void quicksort(int a[],int s,int t)
     6 {
     7     int i=s,j=t;
     8     int tmp = a[s];
     9     if(s<t){    //区间内元素剩0个或者1个的时候停止
    10         while(i<j){
    11             while(i<j && a[j]>=tmp)
    12                 j--;
    13             a[i] = a[j];
    14             while(i<j && a[i]<=tmp)
    15                 i++;
    16             a[j] = a[i];
    17         } 
    18         a[i] = tmp;
    19         quicksort(a,s,i-1);    //对左区间递归排序 
    20         quicksort(a,i+1,t);    //对右区间递归排序 
    21     }
    22 }
    23 int main()
    24 {
    25     int i;
    26     int n,m;
    27     while(cin>>n>>m){
    28         for(i=1;i<=n;i++)
    29             scanf("%d",&a[i]);
    30         quicksort(a,1,n);
    31         for(i=n;i>=n-m+1;i--){
    32             printf("%d",a[i]);
    33             if(i!=n-m+1)
    34                 printf(" ");
    35             else 
    36                 printf("
    ");
    37         }
    38     }
    39     return 0;
    40 }
    Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
    10361233 2014-03-21 19:40:50 Accepted 1425 468MS 2276K 693 B C++ freecode

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    MAVEN 配置阿里云源
    Windows10远程连接CentOS7(搭建Xrdp服务器)
    在jsp显示图片
    unbuntu自动任务定时重启
    eager模式与自定义训练
    JDK安装
    VMware克隆Linux虚拟机注意事项
    系统字符编码
    Iptables防火墙
    查看系统信息
  • 原文地址:https://www.cnblogs.com/yym2013/p/3557883.html
Copyright © 2011-2022 走看看