zoukankan      html  css  js  c++  java
  • 排序降低---------------------

    点击这里进入杭电上的测试题                      点击这里进入厦大的测试题

    杭电的题我用  希尔排序 +Hibbard增量序列 过不去.代码如下(因为是测试的 所以有点乱)

     1 #include<stdio.h>                  //这个直接就跪了   超时/    
     2 #include<algorithm>
     3 using namespace std;
     4 int a[1000011],i,j,m,n,t,d,p,q,tmp;
     5 int main()
     6 {
     7     while(scanf("%d%d",&t,&q)!=EOF)
     8     {
     9         for(i=0;i<t;i++)
    10             scanf("%d",&a[i]);
    11         for(d=(t/2)-1;d>0;d=d/2)  //  从n/2开始 一直向下传送 d直到d=1
    12         {
    13             for(p=d;p<t;p++)   //从这里开始  就是  纯粹的 插入排序了.
    14             {
    15                 tmp=a[p];
    16                 for(i=p;i>=d&&a[i-d]<tmp;i=i-d)
    17                 {
    18                     a[i]=a[i-d];
    19                 }
    20                 a[i]=tmp;
    21             }
    22         }
    23         for(i=0;i<t-1;i++)
    24             printf("%d ",a[i]);
    25         printf("%d
    ",a[t-1]);
    26     }
    27 }

    但是快速排序水过去了.

     1 //差点跪,时间是 904  差一点点跪.  这个好像不是 完整的快排     有待补充
     2 #include<stdio.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int a[1000011],i,j,m,n,t,d,p,q,tmp;
     6 int main()
     7 {
     8     while(scanf("%d%d",&t,&q)!=EOF)
     9     {
    10         for(i=0;i<t;i++)
    11             scanf("%d",&a[i]);
    12         sort(a,a+t);
    13         for(i=t-1;i>t-q;i--)
    14             printf("%d ",a[i]);
    15         printf("%d
    ",a[t-q]);
    16     }
    17 }

    这是用stl的快速排序  别人的代码 //我还没看到  以后会重新过来写.

     1 #include <stdio.h>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 
     6 static int a[1000000];
     7 
     8 int main()
     9 {
    10     int i,n,m;
    11     while(EOF != scanf("%d %d",&n,&m))
    12     {
    13         for(i=0;i<n;i++)
    14             scanf("%d",&a[i]);
    15         make_heap(a,a+n);
    16         printf("%d",a[0]);
    17         for(i=1;i<m;i++)
    18         {
    19             pop_heap(a,a+n-i+1);
    20             printf(" %d",a[0]);
    21         }
    22         printf("
    ");
    23     }
    24     return 0;
    25 }

    至于厦大的   希尔 和 快排

     1 #include<stdio.h>
     2 #include<algorithm>
     3 using namespace std;
     4 int a[1000011],i,j,m,n,t,d,p,tmp;
     5 int main()
     6 {
     7     while(scanf("%d",&t)!=EOF)
     8     {
     9         for(i=0;i<t;i++)
    10             scanf("%d",&a[i]);
    11         for(d=(t/2)-1;d>0;d=d/2)  //  从n/2开始 一直向下传送 d直到d=1
    12    {
    13        for(p=d;p<t;p++)   //从这里开始  就是  纯粹的 插入排序了.
    14        {
    15            tmp=a[p];
    16            for(i=p;i>=d&&a[i-d]>tmp;i=i-d)
    17            {
    18                 a[i]=a[i-d];
    19            }
    20            a[i]=tmp;
    21        }
    22    }
    23         for(i=0;i<t-1;i++)
    24             printf("%d ",a[i]);
    25         printf("%d
    ",a[t-1]);
    26     }
    27 }
    28 /**************************************************************
    29     Problem: 1004
    30     User: xpower
    31     Language: C++
    32     Result: Accepted
    33     Time:700 ms
    34     Memory:4940 kb
    35 ****************************************************************/
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int a[1000011],i,j,m,n,t;
    int main()
    {
        while(scanf("%d",&t)!=EOF)
        {
            for(i=0;i<t;i++)
                scanf("%d",&a[i]);
            sort(a,a+t);
            for(i=0;i<t-1;i++)
                printf("%d ",a[i]);
            printf("%d
    ",a[t-1]);
        }
    }
    /**************************************************************
        Problem: 1004
        User: xpower
        Language: C++
        Result: Accepted
        Time:508 ms
        Memory:4944 kb
    ****************************************************************/
  • 相关阅读:
    FJSC2020合集
    考试前注意事项
    CSP-S&&NOIP2020游记
    IOI2020集训队作业题单
    对拍程序
    Re:memset 赋值
    2019-12-29 Div.3模拟赛题解
    NOI2020 游记
    边三连通分量算法
    【题解】Code+7 教科书般的亵渎
  • 原文地址:https://www.cnblogs.com/A-FM/p/5159573.html
Copyright © 2011-2022 走看看