求n个数中前k个数,按之前的顺序输出(HK IPM)

For smaller k:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <list>
 6 #include <stack>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <iostream>
13 using namespace std;
14 #define ll long long
15 const long maxn=1e6+5;
16 const ll mod=1e9+7;
17 #define inf 1e9
18 
19 struct cmp
20 {
21     bool operator() (pair<long long,long> a,pair<long long,long> b)
22     {
23         return a.first < b.first;
24     }
25 };
26 
27 bool cmp1(pair<long long,long> a,pair<long long,long> b)
28 {
29     return a.second < b.second;
30 }
31 
32 priority_queue<pair<long long,long>,vector<pair<long long,long> >, cmp> f;
33 pair<long long,long> pr[maxn];
34 
35 int main()
36 {
37     long n,k,i,g;
38     long long a;
39     scanf("%ld%ld",&n,&k);
40     for (i=1;i<=n;i++)
41     {
42         scanf("%lld",&a);
43         f.push(make_pair(a,i));
44         if (i>k)
45             f.pop();
46     }
47     g=0;
48     while (!f.empty())
49     {
50         pr[g]=f.top();
51         g++;
52         f.pop();
53     }
54     sort(pr,pr+g,cmp1);
55     for (i=0;i<g;i++)
56         printf("%lld\n",pr[i].first);
57     return 0;
58 }

 

posted @ 2018-04-21 23:43  congmingyige  阅读(242)  评论(0编辑  收藏  举报