60,后两点RE

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

zt17 @ 2023-01-30 17:52:33

#include <bits/stdc++.h>
using namespace std;
int a[100001];
void mysort(int left,int right)
{
    if(left >= right) return;
    int i=left,j=right,flag=a[(left+right)/2],tmp;
    do
    {
        while (a[i] < flag)i++;
        while (a[j] > flag)j--;
        if(i <= j) tmp=a[i],a[i]=a[j],a[j]=tmp,i++,j--;
    } while (i <= j);
    if(left < j)mysort(left,j);
    if(i < right)mysort(i,right);
}
int main()
{
    int n,k;
    cin >> n >> k;
    for(int i=0;i<n;i++)
        cin >> a[i];
    mysort(0,n-1);
    cout << a[k];
    return 0;
}

by Sun_Email @ 2023-01-30 17:56:24

n不止1e5吧


by jiangfeiyang @ 2023-01-30 18:10:16

n我用的是5e6.

但是你这个还要提一下速(用scanf或快读)


by dengsq520 @ 2023-02-12 09:36:26

啊啊啊,我用的特简单,但和你一样,哈哈哈

#include<bits/stdc++.h>
using namespace std;

bool cmp(int i,int j)
{   
return i < j;
}

int main()
{
int n, k;
cin >> n >> k;

vector<int> nums;
int num;

for(int i = 0;i < n;i++)
{
    cin >> num;
    nums.push_back(num);
}

sort(nums.begin(),nums.end(),cmp);

cout << nums[k];

return 0;
}

有没有大佬一起看一下


|