如果你只有60分

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

___LuXun___ @ 2024-08-08 17:58:51

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

ll n, k;
ll a[5000050];

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    cout << a[k + 1];
}

60分

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

ll n, k;
ll a[5000050];

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a + n );
    printf("%d", a[k]);
}

AC

结论:需要格式化输入输出


by LYZ0602 @ 2024-08-08 18:17:21

@LuXun额······我为什么不需要

#include<bits/stdc++.h> 
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a[5000005],i,n,k;
    cin>>n>>k;
    for(i=0;i<=n;i++)
        cin>>a[i];
    sort(a,a+n);
    cout<<a[k];
    return 0;
}

by LYZ0602 @ 2024-08-08 18:22:49

帮你改了一下@LuXun```cpp

include <bits/stdc++.h>

define ll long long

using namespace std;

ll n, k; ll a[5000050];

int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i <= n; i++) { cin >> a[i]; } sort(a , a + n ); cout << a[k ]; }


by sbsbsbfluhang @ 2024-08-08 18:23:02

cin,cout的效率很慢,因为cin,cout它需要自动识别读入类型

但scanf和printf它是输出时,你指定了类型所以快一些

只需要在代码主函数最前面加入

ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll n, k;
ll a[5000050];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    cout << a[k + 1];
}

就能将cin,cout的效率提高至scanf,printf

@LuXun


by LYZ0602 @ 2024-08-08 18:31:26

@sbsbsbfluhang

cout.tie(0);

不用加吧


by liuhuayang @ 2024-09-14 22:43:32

@LYZ0602
希望更丰富的展现?使用 Markdown、KaTeX。


by LYZ0602 @ 2024-09-15 18:20:26

@liuhuayang 我懒


by liuhuayang @ 2024-09-15 18:22:07

……


|