题目
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(≤). Then the next line contains N distinct positive integers no larger than . 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
题目描述
题目给出了一个数列,判断其中哪些数是主元。(在主元之前的数比其小,在主元之后的数比其大)
题目分析
这题目很直观朴素的一种解法,枚举每一个数字,然后再分别从头和尾枚举记录比该数小和大的个数,然后个数相加,若个数=数列个数-1,则该数字为主元;否则,该数字则不是主元。这样做的时间复杂度约为:,在该题目的数据规模下,这样做是会超时的;也曾试过提交过该种朴素方式的代码,最后结果仅通过一个测试点,其他均为超时。
其实,朴素方法耗时的点在于对前后比其小和大的数的统计过程,耗费时间,这里可以通过使用递推关系,来简化这个过程。主元的性质:比它前面的数大,比它后面的数小;换个角度就是:比它前面最大的数大,比它后面最小的数小,这样的话,可以设定一个数组maxLeft[i]来记录从0~i-1的数中的最大值,而maxLeft[i]的更新方式:
同理,可用相似的方法来更新minRight[i];然后,就是数组从到尾扫描,若遇到maxLeft[i] < num[i]且num[i] < minRight[i],则输出相应的num[i]。
#include<stdio.h>
#include<string.h>
const int INF = 0x3fffffff;
int N;
int num[100010], result[100010];
int maxLeft[100010], minRight[100010];
int main() {
int res = 0;
int j;
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", &num[i]);
memset(maxLeft, 0, sizeof(maxLeft));
memset(minRight, 0, sizeof(minRight));
for (int i = 1; i < N; i++) {
if (num[i - 1] > maxLeft[i - 1])
maxLeft[i] = num[i - 1];
else
maxLeft[i] = maxLeft[i - 1];
}
minRight[N - 1] = INF;
for (int i = N - 2; i >= 0; i--) {
if (num[i + 1] > minRight[i + 1])
minRight[i] = minRight[i + 1];
else
minRight[i] = num[i + 1];
}
j = 0;
for(int i = 0; i < N; i++) {
if (maxLeft[i] < num[i] && num[i] < minRight[i]) {
result[j++] = num[i];
res++;
}
}
printf("%d\n", res);
if (res == 0)
putchar('\n');
for (int i = 0; i < j; i++)
if (i != j - 1)
printf("%d ", result[i]);
else
printf("%d", result[i]);
return 0;
}
该题目主要利用递推关系,将原本需要时间来进行搜索判断变成线性扫描并且只是单一的比较操作即可完成。而这种递推关系,主要是利用若该数字比前面序列数字的最大值大,那其就是最大值这种性质推导得出。
这一题与1093A的题目一样,都是需要利用某些性质,来得到一些递推关系,从而减少需要两重循环,以线性扫描的效率来统计某些信息。
网友评论