1045. Quick Sort (25)

Source: Internet
Author: User
Tags print format

Original title: https://www.patest.cn/contests/pat-b-practise/1045

Idea: Looks like the last question of Serie B, if the complexity is n^2, is basically wrong, good
It is also a time-out rhythm, the beginning of their own ideas complex.

Let's go straight to the test given by the topic.

输入样例:51 3 2 4 5输出样例:31 4 5

To determine if a number is a principal, make sure the value on the left side of the number is smaller than the current number,
And the value on the right is bigger than the current number (this is not nonsense!)
If we have 2 additional arrays, which correspond to the current array one by one, each holds the current number to the left
The maximum and the current number to the right of the minimum value, so simple comparison is OK.
We look at the test given, first 1 left no number, maybe everyone would like to think 1 of the left most
The decimal number is 0, and the right maximum is 2. So 0 1 2, 1 is the main element we need. But I
We do not think so now, we put 1 in itself to consider, (this is mainly for programming convenience)

Consider themselves, then the 1 left the largest number is 1, 1 right the smallest number or 1, corresponding
The above test example, we need such an array

原来的: 1 3 2 4 5左边的: 1 3 3 4 5右边的: 1 2 2 4 5

Yes, you will find that if the current number is the main element, it is equal to the left and right. Finally, the most critical
The question is how to draw these 2 arrays, think on paper, or look at the code below,
I'm sure you'll write it.

Pit 1: If all the numbers are not primary, enter 2 lines, respectively, 0 and\n

Realize:

#include <stdio.h>intMain (void) {intNintarr[100010];intlmax[100010];intrmin[100010];intCount =0;intMaxintMinintICharch = "; scanf"%d", &n); for(i =0; I < n; i++) {scanf ("%d", &arr[i]); }//Starting from the left, step by step to find the maximum number left of the current numbermax = arr[0];//Initial Maximum number     for(i =0; I < n; i++) {if(Arr[i] > Max)        {max = arr[i];    } Lmax[i] = max; }//Starting from the right, step by step to find the minimum number of the right of the current numberMin = arr[n-1];//Initial minimum number     for(i = n-1; I >=0; i--) {if(Arr[i] < min)        {min = arr[i];    } Rmin[i] = min; }//The next printing also has to waste some effort, first of all to traverse the number of main elements out,    //And the non-principal is marked as 0     for(i =0; I < n; i++) {if(Arr[i] = = Lmax[i] && arr[i] = = Rmin[i])        {count++; }Else{Arr[i] =0; }} printf ("%d\ n", count);//Then use Count to control print format    //Note empty in C language corresponds to     for(i =0; I < n && count! =0; i++) {if(Count = =1) ch = ' \0';if(Arr[i]! =0) {printf ("%d%c", Arr[i], ch);        count--; }} printf ("\ n");return 0;}

Reference: HTTP://WWW.JIANSHU.COM/P/2927E519D9C5

1045. Quick Sort (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.