/ Vijos / 讨论 / 第k大 /

好容易超时啊

这道题竟然超时了!
程序如下:
var n,k,i,t,j:longint; a:array[0..1000000] of longint;
begin
readln(n,k);
for i:=1 to n do
read(a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
for i:=1 to n do
if i=k then writeln(a[i]);
end.
求大神解题

6 条评论

  • @ 2017-07-21 12:55:55

    pascal计数排序
    #1 Accepted 1ms 204.0KiB
    #2 Accepted 1ms 380.0KiB
    #3 Accepted 1ms 328.0KiB
    #4 Accepted 1ms 344.0KiB
    #5 Accepted 1ms 328.0KiB
    #6 Accepted 1ms 368.0KiB
    #7 Accepted 12ms 328.0KiB
    #8 Accepted 11ms 348.0KiB
    #9 Accepted 10ms 372.0KiB
    #10 Accepted 7ms 328.0KiB

  • @ 2016-08-27 09:05:02

    这种题目用选择、冒泡肯定会超时@!

  • @ 2016-08-27 09:04:24

    试试快排
    呵呵

  • @ 2016-08-15 12:24:42

    评测状态 Accepted
    题目 P1788 第k大
    递交时间 2016-08-15 12:22:20
    代码语言 C++
    评测机 ShadowShore
    消耗时间 421 ms
    消耗内存 956 KiB
    评测时间 2016-08-15 12:22:22

    评测结果

    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 952 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 952 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 952 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 952 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 956 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 952 KiB, score = 10

    测试数据 #6: Accepted, time = 125 ms, mem = 952 KiB, score = 10

    测试数据 #7: Accepted, time = 109 ms, mem = 952 KiB, score = 10

    测试数据 #8: Accepted, time = 109 ms, mem = 952 KiB, score = 10

    测试数据 #9: Accepted, time = 78 ms, mem = 952 KiB, score = 10

    Accepted, time = 421 ms, mem = 956 KiB, score = 100

    代码

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n,k;
    int score[100001];
    int main()
    {
    cin>>n>>k;
    for(int i=0;i<n;i++)
    cin>>score[i];
    sort(score,score+n,greater<int>());
    k--;
    cout<<score[k];
    return 0;
    }
    C++大好

  • @ 2016-07-31 08:59:58
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #define min(a,b) (a>b?b:a)
    #define max(a,b) (a>b?a:b)
    
    using namespace std;
    typedef long long lg;
    
    priority_queue<int>q;
    int n,k,x;
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin>>n>>k;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            q.push(x);
        }
        for(int i=1;i<k;i++)
            q.pop();
        cout<<q.top();  
        return 0;
    }
    

    转c++吧,骚年!

  • @ 2016-05-20 13:31:05
    评测结果
    编译成功
    
    测试数据 #0: Accepted, time = 31 ms, mem = 940 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 940 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 936 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 940 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 940 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 940 KiB, score = 10
    测试数据 #6: Accepted, time = 46 ms, mem = 940 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 940 KiB, score = 10
    测试数据 #8: Accepted, time = 46 ms, mem = 940 KiB, score = 10
    测试数据 #9: Accepted, time = 31 ms, mem = 936 KiB, score = 10
    Accepted, time = 169 ms, mem = 940 KiB, score = 100
    代码
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    int main()
    {
      int i,n,k,a[100001];
      scanf("%d %d",&n,&k);
      for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
      sort(a+1,a+n+1,greater<int>());
      printf("%d",a[k]);
      system("pause");
      return 0;
    }
    

    转c++吧,骚年!
    自带sort函数。

  • 1

信息

ID
1788
难度
5
分类
模拟 点击显示
标签
(无)
递交数
2037
已通过
646
通过率
32%
被复制
2
上传者