ruanertanjing @ 2024-08-03 23:27:30
// P1923 【深基9.例4】求第 k 小的数
// https://www.luogu.com.cn/problem/P1923
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <functional>
#include <climits>
#define quickio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
typedef long long ll;
const int N = 5000000;
int a[N];
int n, k;
// 优先队列
void solve()
{
// 注意优先队列的写法
// 这样子写才是升序,小根堆
// 默认大根堆,是降序
priority_queue<int, vector<int>, greater<int>>que;
for (int i = 0; i < n; i++)
{
cin >> a[i];
que.push(a[i]);
}
k--;
while (k--)
que.pop();
cout << que.top();
}
int main()
{
quickio;
cin >> n >> k;
solve();
return 0;
}
by liaocr @ 2024-08-03 23:34:35
@ruanertanjing emm 最后写错了 这样变成第k大了
by liaocr @ 2024-08-03 23:36:22
@ruanertanjing 哦 脑子抽了 别管我
by liaocr @ 2024-08-03 23:39:18
@ruanertanjing 找到了题目说最小的数是第0小 (有点坑人了) 所以把k--去掉就AC了
by liaocr @ 2024-08-03 23:40:09
@ruanertanjing AC代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <functional>
#include <climits>
#define quickio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
typedef long long ll;
const int N = 5000500;
int a[N];
int n, k;
// 优先队列
void solve()
{
// 注意优先队列的写法
// 这样子写才是升序,小根堆
// 默认大根堆,是降序
priority_queue<int, vector<int>, greater<int>>que;
for (int i = 0; i < n; i++)
{
cin >> a[i];
que.push(a[i]);
}
k--;
while (k--)
que.pop();
cout << que.top();
}
int main()
{
quickio;
cin >> n >> k;
solve();
return 0;
}
可以的话给个关注 不给也行吧qwq
by ruanertanjing @ 2024-08-03 23:51:05
@liaocr 非常非常感谢?