国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT A1101. Quick Sort (25)

2019-11-08 02:09:43
字體:
來源:轉載
供稿:網友

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:
51 3 2 4 5Sample Output:
31 4 5
#include <cstdio>#include <algorithm>#include <cstring>#define Max 100010const int MAX = 0x7fffffff;using namespace std;int main(){	int n,m=0;	int S[Max],L[Max],R[Max],D[Max];	scanf("%d",&n);	for(int i=0;i<n;i++)	{		scanf("%d",&S[i]);	}	 L[0]=0;	for(int i=1;i<n;i++)	{				L[i]=max(L[i-1],S[i-1]);			}	R[n-1]=MAX;	for(int i=n-2;i>=0;i--)	{					R[i]=min(R[i+1],S[i+1]);			}	for(int i=0;i<n;i++)	{		if(L[i]<S[i]&&R[i]>S[i])		{			D[m++]=S[i];		}	}	//sort(D,D+m);	printf("%d/n",m);	for(int i = 0; i< m; i++)	{		printf("%d",D[i]);		if(i<m-1) printf(" ");			}	printf("/n");	system("pause");	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万安县| 陆川县| 民丰县| 昌宁县| 阳西县| 抚州市| 大同市| 永德县| 宜兰县| 武陟县| 西宁市| 游戏| 云和县| 轮台县| 白山市| 宜丰县| 安康市| 高州市| 资源县| 星子县| 葵青区| 祁阳县| 北辰区| 万年县| 育儿| 含山县| 大同县| 沈阳市| 唐山市| 合水县| 大港区| 称多县| 水富县| 嘉荫县| 安福县| 博野县| 咸宁市| 井陉县| 五台县| 张家界市| 翁牛特旗|