为什么超时,求救

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

$1 \leq n \leq 5 \times 10^6$的话就不要在`main`函数内定义`a`数组了 ~~验证码是2ace,是两次AC还是两次CE呢?~~
by SwethessPotion @ 2024-02-19 18:50:02


@[SwethessPotion](/user/1059747) 是这样吗? ```cpp #include<bits/stdc++.h> using namespace std; int maxn(int a[],int n,int k) { bool p=true; int x,num,i=0; while (p) { x=a[i]; num=0; for(int j=0;j<=n;i++){ if(x>a[j]) num++; } if(num==k) p=false; else i++; } return x; } int a[10000000]; int main(){ int n,k; cin>>n>>k; for(int i=0;i<n;i++) cin>>a[i]; cout<<maxn(a,n,k); return 0; } ```
by lucy2012 @ 2024-02-19 19:53:08


@[lucy2012](https://www.luogu.com.cn/user/1252442) 是的,记住以后很大的数组不能再函数里开,不然容易承受不住 附赠一张MLE表: | 数据类型 | 开$10^3$数组占用MB | | :----------: | :----------: | | `int` | $0.0038$ | | `long long` | $0.0076$ | | `bool`和`char` | $0.0009$ | | `string` | $0.01$左右 | - 二维数组顶多开到$5000$! - 一维数组最多开$3 \times 10^7$,最好把上限想成$10^7$
by SwethessPotion @ 2024-02-19 20:15:07


记得关闭ios::sync_with_stdio(false); 不然也会超时
by Xwit @ 2024-02-29 16:47:19


|