1101 Quick Sort (25 分)(超时,格式错误原因)

360 篇文章 3 订阅
91 篇文章 1 订阅

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

正如题目的标题,本题采用与快速排序类似的思路,我们知道快速排序每次能够选择出一个元素,左边的都小,右边的都大,那么本题,如果排序好的数组与原数组中,位置没有发生变化,就说明该数字是符合条件的,我们只需要判断排序好的数组与原数组的关系
但是,有特殊情况,当某个元素,左右两边的元素相等时,比如 5 1 3 4 2,排好序变成 1 2 3 4 5,此时,3 的位置不变,但是不满足题意,解决的办法也比较简单,维护一个左边最大的 max,当某个数位置不变并且大于 max 的时候,说明排好序的,每次更新 左边最大的 max,举例:此时该数字是 3,左边的最大 max 是 5,不满足大于 max,所以 3 就不满足题意

格式错误原因是因为,如果 count 等于 0,需要单独输出一个回车

#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<cstring>
using namespace std;
int n;
int a[100001];
int b[100001];
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		b[i] = a[i];
	}
	vector<int> v;
	int count = 0;
	sort(b, b + n);
	int max = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == b[i] && a[i] > max) {
			v.push_back(a[i]);
			count++;
		} 
		if (a[i] > max) {
			max = a[i];
		}
	}
	cout << count << endl;
	if (count) { 
		for (int i = 0; i < v.size(); i++) {
			if (!i) {
				cout << v[i];
			} else {
				cout << " " << v[i];
			}
		}
	} else {
		cout << endl;
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值