PAT1101A

题目

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. 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(≤10^5). Then the next line contains N distinct positive integers no larger than 10^9. 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,则该数字为主元;否则,该数字则不是主元。这样做的时间复杂度约为:O(n^2),在该题目的数据规模下,这样做是会超时的;也曾试过提交过该种朴素方式的代码,最后结果仅通过一个测试点,其他均为超时。
   其实,朴素方法耗时的点在于对前后比其小和大的数的统计过程,耗费时间,这里可以通过使用递推关系,来简化这个过程。主元的性质:比它前面的数大,比它后面的数小;换个角度就是:比它前面最大的数大,比它后面最小的数小,这样的话,可以设定一个数组maxLeft[i]来记录从0~i-1的数中的最大值,而maxLeft[i]的更新方式:
maxLeft[i]=\begin{cases}maxLeft[i-1] &num[i-1] < maxLeft[i-1] \cr num[i-1], &num[i-1] > maxLeft[i-1]\end{cases}
同理,可用相似的方法来更新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;
}

   该题目主要利用递推关系,将原本需要O(n^2)时间来进行搜索判断变成线性扫描并且只是单一的比较操作即可完成。而这种递推关系,主要是利用若该数字比前面序列数字的最大值大,那其就是最大值这种性质推导得出。
   这一题与1093A的题目一样,都是需要利用某些性质,来得到一些递推关系,从而减少需要两重循环,以线性扫描的效率来统计某些信息。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容