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;
    }
    
  • 相关阅读:
    According to TLD or attribute directive in tag file, attribute end does not accept any expressions
    Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already in use.
    sql注入漏洞
    Servlet—简单的管理系统
    ServletContext与网站计数器
    VS2010+ICE3.5运行官方demo报错----std::bad_alloc
    java 使用相对路径读取文件
    shell编程 if 注意事项
    Ubuntu12.04下eclipse提示框黑色背景色的修改方法
    解决Ubuntu环境变量错误导致无法正常登录
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/9501844.html
Copyright © 2011-2022 走看看