1101. Quick Sort (25)

37 篇文章 0 订阅
29 篇文章 0 订阅

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
  • N个数
  • 对于这N个数,那些左边都小于等于它,右边都大于等于它的是属于要输出的,进入ans中
  • ans排好序从小到大
  • 输出ans中有几个数+换行
  • 输出ans[0] ans[1]……+换行(PS即使没有数也要换行(否则一个测试点错误),如果有数,每两个数要空格,最后的数后面没有空格)
  • 这次pat2015考试中是B题,由于以前对于qsort些起来不熟,以为要写qsort没有认真看。这题就是这次pat中连错误代码都没有提交的。考完仔细一看,根本不用qsort 的partition

    评测结果

    时间结果得分题目语言用时(ms)内存(kB)用户
    9月14日 21:24答案正确251101C++ (g++ 4.7.2)431708datrilla

    测试点

    测试点结果用时(ms)内存(kB)得分/满分
    0答案正确130812/12
    1答案正确4017082/2
    2答案正确245644/4
    3答案正确4310243/3
    4答案正确426922/2
    5答案正确428202/2
    #include<iostream>   
    #include<vector>   
    using namespace std; 
    int main()
    {
      int N, i,leftmax_rightmin;
      cin >> N;
      vector<int>qqsot(N);
      vector<bool>flag(N, true);
      vector<int>ans;
      for (i = 0; i < N; i++)
      {
        cin >> qqsot[i];
        if (0 == i)leftmax_rightmin = qqsot[i];
        else  if (leftmax_rightmin > qqsot[i])
          flag[i] = false;
        else leftmax_rightmin = qqsot[i];
      }
      for (i = N - 1, leftmax_rightmin = qqsot[N - 1]; i >= 0; i--)
      {  
        if (leftmax_rightmin<qqsot[i])
          flag[i] = false;
        else
        {
          leftmax_rightmin = qqsot[i];
          if (flag[i]) ans.push_back(qqsot[i]);
        }
      }
      N = ans.size();
      cout << N-- << endl;
      if (!ans.empty())
      { 
        cout << ans[N];
        while(N--)cout << " " << ans[N];
      }
      cout << endl;
      system("pause");
      return 0;
    }

    评测结果

    时间结果得分题目语言用时(ms)内存(kB)用户
    9月12日 23:36答案正确251101C++ (g++ 4.7.2)521836datrilla

    测试点

    测试点结果用时(ms)内存(kB)得分/满分
    0答案正确130812/12
    1答案正确5218362/2
    2答案正确245644/4
    3答案正确449483/3
    4答案正确428242/2
    5答案正确428202/2
    #include<iostream>   
    #include<vector>  
    #include<algorithm>
    using namespace std;  
    bool anscmp(const int  &A, const int  &B){return A < B; }
    int main()
    {
      int N, i,rightmin,leftmax;
      cin >> N;
      vector<int>qqsot(N); 
      vector<bool>flag(N,true);
      vector<int>ans;
      for (i = 0; i < N; i++)
      {
        cin >> qqsot[i];
        if (0 == i)leftmax = qqsot[i];
        else  if(leftmax > qqsot[i])
         flag[i]=false;
        else leftmax =qqsot[i]; 
      }
      for (i = N-1; i >=0; i--)
      { 
        if (N - 1 == i)rightmin = qqsot[i];
        else  if(rightmin<qqsot[i])
        flag[i]=false;
      else rightmin =  qqsot[i];
        if (flag[i])
          ans.push_back(qqsot[i]);
      }
      N = ans.size();
      cout <<N<<endl;
      if (!ans.empty())
      {
        sort(ans.begin(), ans.end(), anscmp);
        cout << ans[0]; 
        for (i = 1; i < N; i++)cout <<" "<< ans[i]; 
      }
      cout << endl;
      system("pause");
      return 0;
    }
  • 评测结果

    时间结果得分题目语言用时(ms)内存(kB)用户
    9月12日 20:59答案正确251101C++ (g++ 4.7.2)512452datrilla

    测试点

    测试点结果用时(ms)内存(kB)得分/满分
    0答案正确130812/12
    1答案正确5124522/2
    2答案正确259484/4
    3答案正确4417163/3
    4答案正确4215882/2
    5答案正确4215922/2
    #include<iostream>   
    #include<vector>  
    #include<algorithm>
    using namespace std;  
    bool anscmp(const int  &A, const int  &B){return A < B; }
    int main()
    {
      int N, i;
      cin >> N;
      vector<int>qqsot(N);
      vector<int>rightmin(N);
      vector<int>leftmax(N);
      vector<int>ans;
      for (i = 0; i < N; i++)
      {
        cin >> qqsot[i];
        if (0 == i)leftmax[i] = qqsot[i];
        else   leftmax[i] = leftmax[i - 1] > qqsot[i] ? leftmax[i - 1] : qqsot[i]; 
      }
      for (i = N-1; i >=0; i--)
      { 
        if (N - 1 == i)rightmin[i] = qqsot[i];
        else   rightmin[i] = rightmin[i + 1]<qqsot[i] ? rightmin[i+1] : qqsot[i];
        if (leftmax[i]<=qqsot[i]&&rightmin[i]>=qqsot[i])
          ans.push_back(qqsot[i]);
      }
      N = ans.size();
      cout <<N<<endl;
      if (!ans.empty())
      {
        sort(ans.begin(), ans.end(), anscmp);
        cout << ans[0]; 
        for (i = 1; i < N; i++)cout <<" "<< ans[i]; 
      }
      cout << endl;
      system("pause");
      return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值