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 5Sample Output:
3 1 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 long
const 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;
}