惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
P
Proofpoint News Feed
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
C
Check Point Blog
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
美团技术团队
D
Docker
博客园 - Franky
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
PAT乙级 1015.德才论|OhYee 博客
2018-06-13 · via OhYee 博客

题目

原题链接

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:
输入第1行给出3个正整数,分别为:N(<=10^5^),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。

随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0,
100]内的整数。数字间以空格分隔。

输出格式:
输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

解析

时限已经到了Python也过不了的情况了,所以以后看情况决定要不要写Java和Python吧
这道题的收获在于,Python3.x的sort中cmp函数的使用
from functools import cmp_to_key

代码

C++解法

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int maxn = 1e5 + 5;
int n, L, H;

struct Node {
    int id, d, c;
    Node() {}
    Node(int _id, int _d, int _c) : id(_id), d(_d), c(_c) {}

    static int getGrade(const Node stu) {
        if (stu.d >= H && stu.c >= H)
            return 4;
        else if (stu.d >= H && stu.c < H)
            return 3;
        else if (stu.d < H && stu.c < H && stu.d >= stu.c)
            return 2;
        else
            return 1;
    }
    bool operator<(const Node &rhs) const {
        const Node stu1 = *this;
        const Node stu2 = rhs;
        int gstu1 = Node::getGrade(stu1);
        int gstu2 = Node::getGrade(stu2);
        int ret = 0;
        if (gstu1 == gstu2)
            if ((stu1.d + stu1.c) == (stu2.d + stu2.c))
                if (stu1.d == stu2.d)
                    ret = stu2.id - stu1.id;
                else
                    ret = stu1.d - stu2.d;
            else
                ret = (stu1.d + stu1.c) - (stu2.d + stu2.c);
        else
            ret = gstu1 - gstu2;
        return ret > 0 ? true : false;
    }
};

Node stus[maxn];
int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    cin >> n >> L >> H;

    int m = 0;
    for (int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        if (b >= L && c >= L)
            stus[m++] = Node(a, b, c);
    }

    sort(stus, stus + m);

    printf("%d\n", m);
    for (int i = 0; i < m; ++i)
        printf("%d %d %d\n", stus[i].id, stus[i].d, stus[i].c);

    return 0;
}

Python解法

from functools import cmp_to_key
import re

def compare(stu1, stu2):
    def getGrade(stu):
        if stu[1] >= c and stu[2] >= c:
            return 4
        elif stu[1] >= c and stu[2] < c:
            return 3
        elif stu[1] < c and stu[2] < c and stu[1] >= stu[2]:
            return 2
        else:
            return 1
    gstu1 = getGrade(stu1)
    gstu2 = getGrade(stu2)
    ret = 0
    if gstu1 == gstu2:
        if (stu1[1] + stu1[2]) == (stu2[1] + stu2[2]):
            if stu1[1] == stu2[1]:
                ret = stu2[0] - stu1[0]
            else:
                ret = stu1[1] - stu2[1]
        else:
            ret = (stu1[1] + stu1[2]) - (stu2[1] + stu2[2])
    else:
        ret = gstu1 - gstu2
    return ret

def inputInt():
    return [int(i) for i in re.findall(r"[0-9]+",input())]


(a, b, c) = inputInt()
lst = []
for i in range(a):
    stu = inputInt()
    if stu[1] >= b and stu[2] >= b:
        lst.append(stu)
lst.sort(key=cmp_to_key(compare), reverse=True)

print(len(lst))
for i in lst:
    print(" ".join([str(j) for j in i]))