题解 | 分数线划定

分数线划定

https://www.nowcoder.com/practice/2395fa7b6c6e452e8d8310a7cfdbe902

//算法练习 No.8
//vector+struct+自定义排序
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct Student{
    int id;
    int score;
};

bool cmp(const Student& a,const Student& b)
{
    if (a.score != b.score)
    {
        return a.score > b.score;
    }
    return a.id < b.id;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n,m;
    cin >> n >> m;
    int t = floor(m * 1.5);

    vector<Student> v(n);
    for(int i=0;i<n;i++)
    {
        cin >> v[i].id >> v[i].score;
    }

    sort(v.begin(),v.end(),cmp);
   
   int line_score = v[t-1].score;
   int line_cnt {0};
   for(const auto& stu :v)
   {
        if(stu.score >= line_score)
            line_cnt++;
        else
            break;
   }

   cout << line_score << " " <<line_cnt << "\n";
   for(int i = 0;i < line_cnt; i++)
   {
        cout << v[i].id << " " << v[i].score << "\n";
   }

    return 0;
}

全部评论

相关推荐

牛客28967172...:跟着卡子哥才是正道,灵茶属实不太行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务