zoukankan      html  css  js  c++  java
  • PAT 1101 Quick Sort

    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 (<= 10^5^). Then the next line contains N distinct positive integers no larger than 10^9^. 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

    分析
    这道题就是求一列数中比左边所有数大,比右边所有数小的数是哪些,其实只需要创建两个vector MAX, MIN.
    MAX[i]表示vec[i]左边的所有数中最大的数,
    MIN[i]表示vec[i]右边的所有数中最小的数。
    其中初始化MAX[0]=0,MIN[N-1]=1000000001;
    MAX[i]=max(MAX[i-1], vec[i-1]);
    MIN[i]=min(MIN[i+1], vec[i+1]);
    如果vec[i]比MAX[i]大,比MIN[i]小,就满足条件。
    特别提醒最后还要输出换行,不然有个测试点过不了

    #include<iostream> //最后要输出换行符
    #include<vector>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int main(){
      int N;
      cin>>N;
      vector<int> vec(N,0);
      for(int i=0; i<N; i++)
        cin>>vec[i];
      vector<int> Max(N,0), Min(N,0);
      Max[0]=0;   Min[N-1]=1000000001;
      for(int i=1; i<N; i++)
        Max[i]=max(Max[i-1], vec[i-1]);
      for(int i=N-2; i>=0; i--)
        Min[i]=min(Min[i+1], vec[i+1]);
      vector<int> ans;
      for(int i=0; i<N; i++)
        if(vec[i]>Max[i]&&vec[i]<Min[i])
          ans.push_back(vec[i]);
      sort(ans.begin(), ans.end());
      cout<<ans.size()<<endl;
      for(int i=0; i<ans.size(); i++)
        i==0?cout<<ans[i]:cout<<" "<<ans[i];
      cout<<endl;
      return 0;
    }
    
  • 相关阅读:
    linux 静态库和动态库(共享库)的制作与使用(注意覆盖问题)转
    手机号码格式正则表达式
    项目去除TFS关联、迁移重部署
    Excel中VLOOKUP函数的用法和注意点
    自定义打赏插件
    分享一个无需注册,无次数限制的Smile聊天机器人接口
    TCP/IP
    Java字典树
    平衡二叉树结构 AVL
    二叉搜索树(二叉排序树)BST
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/9501844.html
Copyright © 2011-2022 走看看