样例正确,但是为什么是0分???!!!

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

zhouyi1227 @ 2020-07-18 17:32:04

求助

#include<bits/stdc++.h>
using namespace std;
int a[5000005];
int main(){
    int n,k,t=0;
    cin >> n >> k;
    for (int i=1;i<=n;i++) cin >> a[i];
    sort(a+1,a+n+1);
    if (k==0){
        cout << a[1];
        return 0;
    }
    for (int i=2;i<=n;i++){
        if (a[i+1]!=a[i]) t++;
        if (t==k){
            cout << a[i];
            return 0;
        }
    }
    return 0;
}

by Dimly_dust @ 2020-07-18 17:33:50

k+1吧


by Dimly_dust @ 2020-07-18 17:35:36

@zhouyi1227 直接输出a[k+1]不好吗??


by zhouyi1227 @ 2020-07-18 17:43:20

@Dimly_dust 现在3个AC,2个TLE


by SIXIANG32 @ 2020-07-18 17:49:12

@zhouyi1227 吸氧


by SIXIANG32 @ 2020-07-18 17:49:40

@zhouyi1227 你都不算算时间复杂度的啊……
这题的数据稳稳爆,建议O2


by Spasmodic @ 2020-07-18 18:00:37

@SIXIANG O2你horse呢

sort(a+1,a+n+1);改成nth_element(a+1,a+k+1,a+n+1);罢,然后写个快读?


by hensier @ 2020-07-18 18:00:55

@zhouyi1227 用scanf或快读优化。


by SIXIANG32 @ 2020-07-18 18:01:31

@happydef 我当时O2+sort鬼畜AC……
现在数据改了?


by SIXIANG32 @ 2020-07-18 18:02:36

论·如何用你谷神机过n \le 5000000 O(n \log_2 n)排序


by Spasmodic @ 2020-07-18 18:03:01

啊不我傻了

random_shuffle一发再nth即可


| 下一页