2 条题解
-
1
利用优先队列实现的小根堆排序;
#include <bits/stdc++.h> #define int long long #define pow_of_two(n) (n & -n == n) using namespace std; #define lowbit(x) (x) & (-x) // const int N = 1e6 + 10; inline void solve() { priority_queue<int , vector<int> , greater<int> > pq; int n , m; cin >> n >> m; for (int i = 1 , temp ; i <= n ; i ++ ) cin >> temp , pq.push(temp); while (m -- ) cout << pq.top() << " " , pq.pop(); cout << endl; return ; } signed main() { ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0); solve(); return 0; }
-
0
参考答案:
#include<iostream> using namespace std; const int N = 200010; int a[N]; int n, m; void down(int u) { int t = u; if (2 * u <= n && a[2 * u] <= a[t]) t = 2 * u; if (2 * u + 1 <= n && a[2 * u + 1] <= a[t]) t = 2 * u + 1; if (t != u) { swap(a[u], a[t]); down(t); } } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = n / 2; i >= 1; --i) down(i); while (m--) { cout << a[1] << ' '; a[1] = a[n--]; down(1); } return 0; }
- 1
信息
- ID
- 77
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 4
- 已通过
- 2
- 上传者