一、题目描述
原题链接
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.
Input Specification:
Output Specification:
Sample Input:
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
Sample Output:
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 -
二、解题思路
结构体排序题,我们将每个考生设置成结构体,包括id,AC的题目数,排名和mk(标记该生有没有能通过编译的提交),还有一个grade数组,表示各题的分数,用grade[0]表示总分。代码还是很好理解的,就是稍微有点长…
三、AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 10010;
struct User
{
int id, perfect=0, rank, mk=-1;//mk: 是否有能通过编译的提交,若没有则不打印
int grade[6] = {0, -1, -1, -1, -1, -1};
}u[maxn];
int p[6];
bool checked[maxn] = {false}; //标记是否有过提交
bool cmp(User a, User b) //排序函数
{
if(a.grade[0] != b.grade[0]) return a.grade[0] > b.grade[0];
else if(a.perfect != b.perfect) return a.perfect > b.perfect;
else return a.id < b.id;
}
int main()
{
vector<User> all;
vector<int> idx;
int N, K, M, tmp, pid, tmpgrade;
scanf("%d%d%d", &N, &K, &M);
for(int i=1; i<=K; i++) scanf("%d", &p[i]);
for(int i=0; i<M; i++)
{
scanf("%d%d%d", &tmp, &pid, &tmpgrade);
u[tmp].id = tmp;
if(tmpgrade != -1) u[tmp].mk = 1; //有可以编译通过的题目
else tmpgrade = 0;
if(tmpgrade > u[tmp].grade[pid])
u[tmp].grade[pid] = tmpgrade;
if(checked[tmp]) continue;
else
{
checked[tmp] = true;
idx.push_back(tmp);
}
}
for(int i=0; i<idx.size(); i++)
{
for(int j=1; j<=K; j++)
{
if(u[idx[i]].grade[j]>=0)
{
u[idx[i]].grade[0] += u[idx[i]].grade[j];
if(u[idx[i]].grade[j] == p[j])
u[idx[i]].perfect++;
}
else continue;
}
if(u[idx[i]].mk > 0) all.push_back(u[idx[i]]);
}
sort(all.begin(), all.end(), cmp);
for(int i=0; i<all.size(); i++)
{
if(i == 0) all[i].rank = 1;
else
{
if(all[i].grade[0] == all[i-1].grade[0]) all[i].rank = all[i-1].rank;
else all[i].rank = i+1;
}
printf("%d %05d", all[i].rank, all[i].id);
for(int j=0; j<=K; j++)
{
if(all[i].grade[j] >= 0) printf(" %d", all[i].grade[j]);
else printf(" -");
}
if(i != all.size()-1)
printf("\n");
}
return 0;
}