zoukankan      html  css  js  c++  java
  • PAT1101:Quick Sort

    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

    思路

    实现快排中取划分点的这一部分代码(而且只是一种最简单的找划分点的方法)。

    题目要求让你统计一组数中有哪些数满足划分点的要求。

    1.从左至右遍历找不满足本身比左侧所有数大的点。

    2.从右到左遍历找不满足本身比右侧所有数小的点(如果之前在从左到右的遍历中这个点已经不满足条件了就不用再计数了)。

    3.打印结果就行

    代码

    #include<iostream>
    #include<vector>
    using namespace std;
    vector<int> nums(100001);
    vector<bool> check(100001,true);
    const int INITMAX = 1000000003;
    const int INITMIN = -233;
    using namespace std;
    int main()
    {
       int N;
       while(cin >> N)
       {
          int countnum = N,lminnum = INITMIN,rmaxnum = INITMAX;
          for(int i = 0;i < N;i++)
          {
              cin >> nums[i];
              if(nums[i] > lminnum)
              {
                  lminnum = nums[i];
                  continue;
              }
              check[i] = false;
              countnum--;
          }
          for(int i = N - 1;i >=0;i--)
          {
              if(nums[i] < rmaxnum)
              {
                  rmaxnum = nums[i];
                  continue;
              }
              if(check[i])  //避免重复计数
              {
                  check[i] = false;
                  countnum--;
              }
    
          }
          bool isFirst = true;
          cout << countnum << endl;
          for(int i = 0;i < N;i++)
          {
              if(check[i])
              {
                  if(isFirst)
                  {
                    cout << nums[i];
                    isFirst = false;
                  }
                  else
                    cout << " " << nums[i];
              }
          }
          cout << endl;
       }
    }
  • 相关阅读:
    leetcode
    leetcode
    leetcode
    leetcode
    Postman
    Fiddler抓百度和bing搜索包
    Jmeter脚本录制
    Android Studio使用Maven国内镜像
    Open Live Writer
    PureXXX使用手记
  • 原文地址:https://www.cnblogs.com/0kk470/p/7689310.html
Copyright © 2011-2022 走看看