PAT 解题报告 1075. PAT Judge (25)

1075. PAT Judge 题目描述：

The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.

For each test case, you are supposed to output the ranklist in the following format:

rank user_id total_score s[1] … s[K]

where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then “-” must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id’s. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

1075. PAT Judge 算法分析：

(1) 根据总分算排名rank, 分数越高rank越前面, rank的值越小

(2) 有了排名以后, 对用户按照rank值的升序排序输出(也就是rank值越小排名越高越在前面)

(3) 如果rank相同, 那么看题目得到满分的题目个数的多少, 得满分题目多的排在前面

(4) 如果满分题目数量也一样的话, 那么就按照id的升序排列.

```#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

struct User {
int iUID;
int iRank;
int iTotalScore;
int aryScore[5];
bool arySubmitted[5];
int iCountPerfectlySolved;
bool bGotAnyProblemPassed;
};

User aryUsers[10005];
int aryFullScore[5];
int N, K, M;

bool comp1(const User &a, const User &b) {
return a.iTotalScore > b.iTotalScore;
}

bool comp2(const User &a, const User &b) {
if(a.iRank != b.iRank)
return a.iRank < b.iRank;
if(a.iCountPerfectlySolved != b.iCountPerfectlySolved)
return a.iCountPerfectlySolved > b.iCountPerfectlySolved;
return a.iUID < b.iUID;
}

void gatherRankStatistics() {
for(int i = 0; i < N; ++i) {
aryUsers[i].iUID = i;
if(aryUsers[i].bGotAnyProblemPassed) {
for(int j = 0; j < K; ++j) {
aryUsers[i].iTotalScore += aryUsers[i].aryScore[j];
if(aryUsers[i].aryScore[j] == aryFullScore[j])
aryUsers[i].iCountPerfectlySolved ++;
}
}
}
}

void sortAndRank() {
std::sort(aryUsers, aryUsers + N, comp1);
aryUsers[0].iRank = 1;
for(int i = 1; i < N; ++i) {
if(aryUsers[i].iTotalScore < aryUsers[i-1].iTotalScore)
aryUsers[i].iRank = i + 1;
else if(aryUsers[i].iTotalScore == aryUsers[i-1].iTotalScore)
aryUsers[i].iRank = aryUsers[i - 1].iRank;
}
std::sort(aryUsers, aryUsers + N, comp2);
}

void doPrint() {
for(int i = 0; i < N; ++i) {
if(false == aryUsers[i].bGotAnyProblemPassed) break;
printf("%d %.5d %d",
aryUsers[i].iRank,
aryUsers[i].iUID + 1,
aryUsers[i].iTotalScore);

for(int j = 0; j < K; ++j) {
if(aryUsers[i].arySubmitted[j]) {
printf(" %d", aryUsers[i].aryScore[j]);
}
else
printf(" -");
}
printf("\n");
}
}

int main() {
memset(aryUsers, 0, sizeof(aryUsers));
scanf("%d %d %d", &N, &K, &M);
for(int i = 0; i < K; ++i)
scanf("%D", &aryFullScore[i]);

int uid, pid, partialScore;
for(int i = 0; i < M; ++i) {
scanf("%d %d %d", &uid, &pid, &partialScore);
uid--;
pid--;
aryUsers[uid].arySubmitted[pid] = true;
if(partialScore >= 0) {
aryUsers[uid].bGotAnyProblemPassed = true;
if(partialScore > aryUsers[uid].aryScore[pid]) {
aryUsers[uid].aryScore[pid] = partialScore;
}
}
}
gatherRankStatistics();
sortAndRank();
doPrint();
return 0;
}
```

1075. PAT Judge 注意点：

Written on March 7, 2014