• 1101. Quick Sort (25)


    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
      

    一个元素,满足前面的比他小,后面的比他大,就可能是主元,正反扫一遍,去掉不可能的。注意如果最后结果是0个,要再加一个换行,不然会格式错误。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    using namespace std;
    int main()
    {
        int n,k=0,d=0;
        int a[100001],b[100001]={0},vis[100001]={0};
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int i=0;i<n;i++)
        {
            if(d<a[i])d=a[i];
            if(a[i]<d)vis[i]=1;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(d>a[i])d=a[i];
            if(a[i]>d)vis[i]=1;
        }
        for(int i=0;i<n;i++)
            if(vis[i]==0)b[k++]=a[i];
        cout<<k<<endl;
        if(k)cout<<b[0];
        for(int i=1;i<k;i++)
            cout<<' '<<b[i];
        cout<<endl;
    }

    对排序又有了更好的理解,第二种办法是进行排序,然后和原来数组对比,如果相同位置元素不同,那么肯定不是主元pivot,如果位置的元素相同也不一定,还得满足比前面元素都大才行(满足这一点,自然就满足比后面元素都小的特点,因为他在正确的位置),比如 1 4 3 2 5,把2和4换一下,顺序就对了,但是3的位置不变,可是3并不满足条件,所以排好序了也要进行判断,但还是很巧妙的。

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        int a[100000],b[100000],maxnum=0,ans[100000],k=0,n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        if(n>0)sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            if(a[i]==b[i]&&a[i]>maxn)ans[k++]=a[i];
            if(b[i]>maxn)maxnum=b[i];
        }
        cout<<k<<endl;
        if(k)cout<<ans[0];
        for(int i=1;i<k;i++)
            cout<<' '<<ans[i];
        cout<<endl;
    }
  • 相关阅读:
    字符串算法总结
    [HAOI2007]反素数
    Poj2689 Prime Distance
    [APIO2010]特别行动队
    [国家集训队]middle
    Typecho博客迁移实战
    Typora + 七牛云图床 简易配置
    教你用快捷键 以管理员身份运行cmd
    Typecho博客插入B站视频
    七牛云图床快捷上传方法
  • 原文地址:https://www.cnblogs.com/8023spz/p/7298690.html
Copyright © 2020-2023  润新知