https://www.luogu.org/problem/P1068
#include<bits/stdc++.h> using namespace std; struct Can { int num; int score; } can[5001]; bool compare1(Can a, Can b) { return a.score > b.score; } bool compare2(Can a, Can b) { return a.num < b.num; } int n, m; int mline, mscore; int head = 0, tail = 1, mid; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> can[i].num >> can[i].score; sort(can, can + n, compare1); //按成绩排序 从大到小 mline = floor(1.5*m); //floor取不大于x的最大整数 人数 mscore = can[mline - 1].score; //面试成绩 因为从0开始的,所以要减一 for (int i = floor(1.5*m); i < n; i++) //查看往后是否存在相同的成绩 if (can[i].score == mscore) //如果存在,加一 mline++; //计算面试总人数 mid = can[0].score; for (int i = 1; i <= mline; i++) { if (can[i].score != mid) { //判断成绩是否相同 sort(can + head, can + tail + head, compare2); //如果没有相同的,相当于给自己排序 head = head + tail; //排头相加 tail = 1; //如果重新开始,要重新定义的。 mid = can[head].score; //重新定义排头 } else tail++; //当相同的时候,tail++,然后给相同的按编号排序 } cout << mscore << " " << mline << endl; for (int i = 0; i < mline; i++) cout << can[i].num << " " << can[i].score << endl; return 0; }