【PAT甲级】1101 Quick Sort (25 分)

题意:

输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数。输出可以作为快速排序枢纽点的个数并升序输出这些点的值。

trick:

测试点2格式错误原因:当答案为0时,需要换行两次??。。。。。这是为何

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[100007];
 5 int b[100007];
 6 int vis[100007];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin.tie(NULL);
10     cout.tie(NULL);
11     int n;
12     cin>>n;
13     for(int i=1;i<=n;++i)
14         cin>>a[i];
15     int cnt=0;
16     int mx=0,mn=2e9;
17     for(int i=1;i<=n;++i){
18         if(a[i]>mx)
19             ++vis[i];
20         mx=max(mx,a[i]);
21     }
22     for(int i=n;i;--i){
23         if(a[i]<mn)
24             ++vis[i];
25         mn=min(mn,a[i]);
26     }
27     for(int i=1;i<=n;++i)
28         if(vis[i]==2)
29             b[++cnt]=a[i];
30     cout<<cnt<<"\n";
31     if(cnt){
32         for(int i=1;i<=cnt;++i){
33             cout<<b[i];
34             if(i<cnt)
35                 cout<<" ";
36         }
37     }
38     if(cnt==0)
39         cout<<"\n";
40     return 0;
41 }

原文地址:https://www.cnblogs.com/ldudxy/p/11972161.html

时间: 2024-11-01 12:10:37

【PAT甲级】1101 Quick Sort (25 分)的相关文章

1101. Quick Sort (25)【快排】——PAT (Advanced Level) Practise

题目信息 1101. Quick Sort (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

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. G

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT甲级——A1101 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. G

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if