小心第 t 个人实际上是下标 t-1 !序列是从0开始的!
向下取整因为范围是正数,因此直接int强制类型转换将小数抹去
多关键字序列使用结构体数组vector<Student>进行存储
多关键字排序使用自定义sort函数分别升序降序排列,联动lambda [](const Student &a, const Student &b){}
大年廿八,立花泷祝牛油们心想事成,马到成功!
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Student{
int k;
int s;
};
int main() {
int n, m;
cin >> n >> m;
int t = (int)(1.5 * m);
vector<Student> volunt(n);
for (int i = 0; i < n; i++) {
cin >> volunt[i].k >> volunt[i].s;
}
sort(volunt.begin(), volunt.end(), [](const Student &a, const Student &b){
if (a.s != b.s) {
return a.s > b.s; // a比b更先,所以如果为true的话,a放在排序的前头,b在后头
}
return a.k < b.k;
});
int cnt = 0;
for (int i = 0; i < n; i++) {
if (volunt[i].s < volunt[t-1].s) {
break;
}
cnt++;
}
cout << volunt[t-1].s << " " << cnt << '\n';
for (int i = 0; i < n; i++) {
if (volunt[i].s < volunt[t-1].s) {
break;
}
cout << volunt[i].k << " " << volunt[i].s << '\n';
}
}

京公网安备 11010502036488号