有两个RE不知道哪里错了,求助一下,谢谢!!

P1923 【深基9.例4】求第 k 小的数

Exile_Code @ 2023-07-17 10:12:36

#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>

vector<int>nums; int k;
int part_quick(int l,int  r) {
    int key = nums[(l + r) >> 1];
    swap(nums[key], nums[r]);
    int i = l; int j;
    for ( j = l; j < r; j++) {
        if (nums[j] <= nums[r]) {
            swap(nums[i++], nums[j]);
        }
    }   
    swap(nums[i], nums[r]);
    return i;
}

void quick(int l, int r) {
    if (r <= l)return;
    int q = part_quick( l, r);
    if (q == k) {
        cout << nums[k] << endl;
        exit(0);
    }
    else if (q < k) quick(q+1,r);
    else quick(l,q-1);
}
int main() {
    int n; cin >> n>>k;
    nums.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &nums[i]);
    }
    quick(0, n - 1);
    return 0;
}

by lovely_fcukh @ 2023-07-17 17:10:40

我认为可以用快排更方便


by san_ri_yue @ 2023-07-18 14:57:26

其实可以用万能头#include<bits/stdc++.h>


|