A5. 分数线划定
2026-06-21 15:02:27
发布于:浙江
1阅读
0回复
0点赞
其实我也陷进去了(坑)
一开始我是这样的(标上我的思路)
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct node {// 结构体表示每个选手
int k, s;
bool operator < (node t) {// 排序的比较函数
return s > t.s
or s == t.s && k < t.k;
}
};
node a[5009];// n最大5000,开5009防止越界
int main() {
ios::sync_with_stdio(0);// 加速
cin.tie(0);
cin >> n >> m;
m = floor(m * 1.5);// 实际录取人数
for (int i = 1; i <= n; ++i)
cin >> a[i].k >> a[i].s;
sort(a + 1, a + n + 1);// 排序
cout << a[m].s << " " << m << "\n";// 分数线 and 实际人数
for (int i = 1; i <= m; ++i)// 被录取人
cout << a[i].k << " " << a[i].s << "\n";
return 0;
}
可是
只有#2和#3是对的
我又把floor改成ceil
只对一个#1
然后
“分数线给了,报名号在后面的几位且分数等于分数线的不能录取,不公平呀!”
于是我又把代码改了
正解来了!
我们在排完序之后把报名号在后面的几位且分数等于分数线的几位也加进来,也就是
while (a[m].s == a[m + 1].s && m <= n)
m++;
然后就像原来的那样输出就可以了
完整代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct node {
int k, s;
bool operator < (node t) {
return s > t.s
or s == t.s && k < t.k;
}
};
node a[5009];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
m = floor(m * 1.5);
for (int i = 1; i <= n; ++i)
cin >> a[i].k >> a[i].s;
sort(a + 1, a + n + 1);
while (a[m].s == a[m + 1].s && m <= n)
m++;
cout << a[m].s << " " << m << "\n";
for (int i = 1; i <= m; ++i)
cout << a[i].k << " " << a[i].s << "\n";
return 0;
}
这里空空如也

有帮助,赞一个