1075 PAT Judge (25分)[排序]

By Jalan

知识工具需求

数学

数据结构和算法

语言

题干

生成PAT排名

输入条件

第一行有3正整数N<=10^4用户总数,K<=5题目总数,M<=10 ^5提交总数,id是五位数,从1到N,问题编号1-K,
下一行K个数,是第i个题的满分数.
下面是M行每行按照这个格式:
user_id problem_id partial_score_obtained
不能通过编译是-1,或者是[0,满分]的一个数字

例1

7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0

输出条件

输出排名按照格式:
rank user_id total_score s[1] … s[K]
rank按总分排,同分同rank,s[i]是第i个的分,如果第i个没提交打印-,提交多次的按最高分算.
按照rank的非递减序列,rank一样按照完美过的数量非递减排列,还是一样按id升序.
如果全没通过编译或者没提交过则不显示在list上,保证list上面至少一个人.

例1

1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -

题解

第一次

思路

  1. 创建一个结构体里面有 id,每题的分数初始-1,完美通过的数量初始0,总分,是否至少有一个通过;创建这个结构体的数组
  2. 输入时不断更新数组,如果第i题这次提交的分比现有的高
  3. 遍历每个结构体指出完美通过的数量和是否有通过编译,同时计算总分
  4. 按规则排序.
  5. 输出

预期时间复杂度

nlogn

编写用时

30分钟,本来16分就写完了,有几个细节debug了好久,脑子不清楚.

代码

CPP
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef struct node
{
    int id;
    int s[6] = {-2, -2, -2, -2, -2, -2}; //-2表示未提交 -1是,没通过-1应该是0分
    bool atleastone = false;
    int sum = 0;
    int perfect = 0;
} node;
int cmp(node a, node b)
{
    if (a.sum > b.sum)
    {
        return 1;
    }
    else if (a.sum == b.sum)
    {
        if (a.perfect > b.perfect)
        {
            return 1;
        }
        else if (a.perfect == b.perfect)
        {
            if (a.id < b.id)
            {
                return 1;
            }
        }
    }
    return 0;
}
int main(int argc, char const *argv[])
{
    //input
    int N, K, M;
    scanf("%d%d%d", &N, &K, &M);
    int p[6];
    vector<node> list(N + 1);
    for (int i = 1; i <= K; i++)
    {
        scanf("%d", &p[i]);
    }
    int id, problem, score;
    for (int i = 0; i < M; i++)
    {
        scanf("%d%d%d", &id, &problem, &score);
        if (list[id].s[problem] < score)
        {
            list[id].s[problem] = score;
        }
    }

    //process
    for (int i = 1; i <= N; i++)
    {
        list[i].id = i;
        for (int j = 1; j <= K; j++)
        {
            if (list[i].s[j] >=0)
            {
                list[i].atleastone = true;
                break;
            }
        }
        for (int j = 1; j <= K; j++)
        {
            if (list[i].s[j] > 0)
            {
                list[i].sum += list[i].s[j];
            }
            if (list[i].s[j] == p[j])
            {
                list[i].perfect++;
            }
        }
    }
    sort(list.begin() + 1, list.end(), cmp);

    //output
    int rank = 0;
    int sameRankContain = 1;
    list[0].sum = -100; //为了下面使用list[i - 1].sum的时候正确显示rank,这里必不能和0号同分
    for (int i = 1; i <= N; i++)
    {
        if (!list[i].atleastone)
        {
            continue;
        }

        if (list[i].sum == list[i - 1].sum) //同分rank不变,计数同分人数
        {
            ++sameRankContain;
        }
        else //不同分时rank增加同分记录器内容,还原同分记录器
        {
            rank += sameRankContain;
            sameRankContain = 1;
        }
        printf("%d", rank);
        printf(" %05d %d", list[i].id,list[i].sum);
        for (int j = 1; j <= K; j++)
        {
            if (list[i].s[j] == -2)
            {
                printf(" -");
            }
            else if (list[i].s[j] == -1)
            {
                printf(" 0");
            }
            else
            {
                printf(" %d", list[i].s[j]);
            }
        }
        printf("\n");
    }
    return 0;
}

运行用时

在这里插入图片描述

结尾

看在我写了这么多注释的份上可以给我点个赞嘛,求求惹=]砰砰砰,给我加点写下去的油呀@.@
也欢迎关注我的CSDN账号呀,接下来两个月我应该会按这个格式更新所有的PTA甲级题目

                                        **开心code每一天**
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值