PAT 1101. Quick Sort (25)
来源:互联网 发布:淘宝售后服务的重要性 编辑:程序博客网 时间:2024/05/21 12:42
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:
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:51 3 2 4 5Sample Output:
31 4 5
题目大意
给一个N的不同正整数组成的序列,让你求序列中一种数,这种数满足:
当前位置的数左边的数都小于当前的数,右边的数都大于当前的数。
求这种数的数目,并且按从小到大的顺序输出各数。
题目解析
正序扫一遍数组,求开始位置到当前位置的最大数;接着倒序扫描数组,求出当前位置到结尾的最小值。
最后扫描一遍数组,比较当前的数是否满足要求,满足的加入答案数组。
最后排一下序输出即可。
有个坑点是:没有pivot时,也要输出一个空行。
#include <cstdio>#include <cmath>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <cstdlib>#include <climits>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define ll long longconst int MAXN = 100000 + 5;const int MAXM = 100000 + 5;const int INF = 0x7f7f7f7f;const int dir[][2] = {0,1,1,0,0,-1,-1,0};template <class XSD> inline XSD f_min(XSD a, XSD b) { if (a > b) a = b; return a; }template <class XSD> inline XSD f_max(XSD a, XSD b) { if (a < b) a = b; return a; }int n;int a[MAXN], mi[MAXN], ma[MAXN];void Getdata(){ int cnt=0, mina=INF, maxa=0; memset(mi, 0x7f, sizeof mi); memset(ma, 0, sizeof ma); for(int i=0; i<n; i++) scanf("%d", &a[i]), mina=f_min(mina, a[i]), maxa=f_max(maxa, a[i]); ma[0]=a[0]; for(int i=1; i<n; i++) ma[i] = f_max(ma[i-1], a[i]);///左边的最大值 mi[n-1]=a[n-1]; for(int i=n-2; i>=0; i--) mi[i] = f_min(mi[i+1], a[i]);///右边最小值 int ans[MAXN], ct=0; if(a[0]==mina) ans[ct++] = a[0];///第一个数单独判断 for(int i=1; i<n-1; i++) if(a[i]>ma[i-1] && a[i]<mi[i+1]) ans[ct++] = a[i]; if(a[n-1]==maxa) ans[ct++] = a[n-1];///最后一个数单独判断 printf("%d\n", ct); for(int i=0; i<ct; i++) printf("%d%c", ans[i], i==(ct-1)?'\n':' '); if(!ct) printf("\n");///没有pivot时也要输出空行}void Solve(){}int main(){ while(~scanf("%d", &n)){ Getdata(); Solve(); } return 0;}
- PAT 1101. Quick Sort (25)
- [PAT]1101. Quick Sort (25)
- 【PAT】1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- pat 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- pat 1101. Quick Sort (25)
- PAT--1101. Quick Sort
- 【PAT】1101. Quick Sort
- PAT-A 1101. Quick Sort (25)
- PAT A 1101. Quick Sort (25)
- 【PAT】【Advanced Level】1101. Quick Sort (25)
- Pat(A) 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25) 已解决
- PAT 甲级 1101. Quick Sort (25)
- pat-a1101. Quick Sort (25)
- PAT A1101. Quick Sort (25)
- 现代软件工程_项目回顾模板
- 自定义UDF
- OpenStack各组件介绍
- vim-multiple-cursors:支持多重选取内容的 Vim 插件
- android 引入sdk
- PAT 1101. Quick Sort (25)
- js自动刷新
- 最近的学习内容
- 在Linux系统下常用软件的安装
- C++中的remap()函数
- 关于多层数据排序的数据设计
- cat的基本使用
- 动态内存申请
- spring开发中加了aop配置标签需注意事项