• pat1101. Quick Sort (25)


    1101. Quick Sort (25)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CAO, Peng

    There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

    For example, given N = 5 and the numbers 1, 3, 2, 4, and 5. We have:

    • 1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
    • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
    • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
    • and for the similar reason, 4 and 5 could also be the pivot.

      Hence in total there are 3 pivot candidates.

      Input Specification:

      Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then the next line contains N distinct positive integers no larger than 109. The numbers in a line are separated by spaces.

      Output Specification:

      For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

      Sample Input:
      5
      1 3 2 4 5
      
      Sample Output:
      3
      1 4 5
      

    提交代码

    学习网址:http://blog.csdn.net/xyt8023y/article/details/48437945

    题目要求找出序列中的所有x,使得x满足≥前面所有的数,≤后面所有的数,这样的x称为快排中的主元。

    为了快速的判断,显然我们需要x左侧的最大值和右侧的最小值,而且他们一直在变动,一个思路是用两个vector或者数组记录每个位置之前最大值、之后最小值,称为maxBefore和minBehind,它们的实现逻辑如下:

    ①第一个元素没有左侧元素,因此maxBefore[0]=-1作为初始化条件,这样就保证了必然满足。

    ②最后一个元素没有右侧元素,因此minBehind[N-1]=INF(注意INF>10的9次方)。

    ③对于中间部分,只需要定义max和min两个变量实时判断赋值,对于maxBefore,在输入过程中完成;minBehind通过一次反向遍历建立。

    建立好了两个表,就可以对每个元素进行查询,满足了存入res,如果res规模大于0,则先输出规模,再输出排序后的序列;否则输出0,因为序列为空,因此需要空一行,也就是两个回车符。

    想到了就比较简单了。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<stack>
     5 #include<set>
     6 #include<map>
     7 #include<queue>
     8 #include<algorithm>
     9 using namespace std;
    10 int before[100005],behind[100005],num[100005];
    11 #define inf 1000000005
    12 int main()
    13 {
    14     //freopen("D:\INPUT.txt","r",stdin);
    15     int n;
    16     scanf("%d",&n);
    17     int i,j;
    18     int maxbefore=-1,minbehind=inf;
    19     for(i=0; i<n; i++)
    20     {
    21         scanf("%d",&num[i]);
    22         before[i]=maxbefore;
    23         if(maxbefore<num[i]){
    24             maxbefore=num[i];
    25         }
    26     }
    27     for(i=n-1; i>=0; i--)
    28     {
    29         behind[i]=minbehind;
    30         if(minbehind>num[i]){
    31             minbehind=num[i];
    32         }
    33     }
    34     queue<int> q;
    35     for(i=0; i<n; i++)
    36     {
    37         if(num[i]>before[i]&&num[i]<behind[i])
    38         {
    39             q.push(num[i]);
    40         }
    41     }
    42     printf("%d
    ",q.size());
    43     if(!q.empty())
    44     {
    45         printf("%d",q.front());
    46         q.pop();
    47     }
    48     while(!q.empty())
    49     {
    50         printf(" %d",q.front());
    51         q.pop();
    52     }
    53     printf("
    ");
    54     return 0;
    55 }

    自己的做法:借助归并排序,排序过程中被交换过的元素,肯定不符合题意。其他都对,只是最后一个样例超时。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<stack>
      5 #include<set>
      6 #include<map>
      7 #include<queue>
      8 #include<algorithm>
      9 using namespace std;
     10 int num[100005],temp[100005];
     11 map<int,bool> mm;
     12 void Merge(int l,int r,int n)
     13 {
     14     int i=l,j=r,k=l;
     15     bool hav=false;
     16     while(i<r&&j<=n)
     17     {
     18         if(num[i]<num[j])
     19         {
     20             temp[k++]=num[i++];
     21         }
     22         else
     23         {
     24             hav=true;
     25             mm[num[j]]=true;
     26             temp[k++]=num[j++];
     27             break;
     28         }
     29     }
     30     if(hav)//
     31     {
     32         while(i<r&&j<=n)
     33         {
     34             if(num[i]<num[j])
     35             {
     36                 mm[num[i]]=true;
     37                 temp[k++]=num[i++];
     38             }
     39             else
     40             {
     41                 mm[num[j]]=true;
     42                 temp[k++]=num[j++];
     43             }
     44         }
     45         while(i<r){
     46             mm[num[i]]=true;
     47             temp[k++]=num[i++];
     48         }
     49     }
     50     while(i<r){
     51         temp[k++]=num[i++];
     52     }
     53     while(j<=n)
     54     {
     55         temp[k++]=num[j++];
     56     }
     57     for(i=l;i<=n;i++){
     58         num[i]=temp[i];
     59     }
     60 }
     61 void MergeSort(int l,int r)
     62 {
     63     if(l<r)
     64     {
     65         int mid=(l+r)/2;
     66         MergeSort(l,mid);
     67         MergeSort(mid+1,r);
     68         Merge(l,mid+1,r);
     69     }
     70 }
     71 int main()
     72 {
     73     //freopen("D:\INPUT.txt","r",stdin);
     74     int n;
     75     scanf("%d",&n);
     76     int i,j;
     77     for(i=0; i<n; i++)
     78     {
     79         scanf("%d",&num[i]);
     80         mm[num[i]]=false;
     81     }
     82     MergeSort(0,n-1);
     83     queue<int> q;
     84     for(i=0; i<n; i++)
     85     {
     86         if(!mm[num[i]])
     87         {
     88             q.push(num[i]);
     89         }
     90     }
     91     printf("%d
    ",q.size());
     92     if(!q.empty())
     93     {
     94         printf("%d",q.front());
     95         q.pop();
     96     }
     97     while(!q.empty())
     98     {
     99         printf(" %d",q.front());
    100         q.pop();
    101     }
    102     printf("
    ");
    103     return 0;
    104 }
  • 相关阅读:
    jquery 中 $.map 的使用方法
    数据库 'MessageManage' 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
    Post提交
    MD5加密、时间戳转换、base64算法加密、解密
    C#中timer类的用法
    软件项目版本号的命名规则及格式
    SQL Server数据库脚本备份与还原
    C# Out,Ref 学习总结
    在线工具
    构造和析构 的顺序
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4842490.html
Copyright © 2020-2023  润新知