题解:P1068 [NOIP2009 普及组] 分数线划定

· · 题解

在这道题中,我们要把分数和编号绑在一起排序,所以要用到结构体。排好序后,再根据题目要求找到所需分数线即可。\ 代码:

#include <bits/stdc++.h>
using namespace std;
struct st
{
    int k, s;
} a[5001];
bool cmp(st aa, st bb) //排序函数
{
    if (aa.s != bb.s)
        return aa.s > bb.s; //如果分数不相等,就把分数高的排在前面
    else
        return aa.k < bb.k; //否则把编号小的排在前面
}
int main()
{
    int m, n;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i].k >> a[i].s;
    }
    sort(a + 1, a + 1 + n, cmp); //排序
    int i;
    m *= 1.5; //计算判定的人的位置
    st x = a[m]; //记录下来
    cout << x.s << ' ';
    for (i = 1; a[i].s >= x.s; i++); //寻找分数大于或等于x的人的数量(也可以用二分)
    cout << i - 1 << endl; //由于多加了一次,所以要输出i-1
    for (i = 1; a[i].s >= x.s; i++) //循环输出进入面试的人
    {
        cout << a[i].k << ' ' << a[i].s << endl;
    }
    return 0;
}