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.

给出PAT的提交记录, 让输出排名, 排名规则见上面.

1075. PAT Judge 算法分析:

模拟题, 就是排名规则啰嗦了点, 仔细理清楚就不难了.

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

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

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

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

特别注意两点, 某个用户对任何一道题目的解答状态有三种: “Not Submitted”, “Submitted but Cannot Compile at All”, “Submitted and at least one of them can be compiled”, 第一点, 如果对于某一题是”Not Submitted”的状态的话对应的输出要显示 ‘-’ 表示没有从来没有提交过, 也就是不算分, 不应该输出零分之类的. 第二点, 能不能编译通过这个信息要用来判断要不要输出这个用户, 题目要求是对于那些没有提交过任何可以编译通过的solution的用户, 我们不予输出. 下面是可以AC的代码:

#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 注意点:

零分表示用户的提交可以编译通过, 和编译通不过要区分出来, 其实某个用户对任何一道题目的解答状态有三种: “Not Submitted”, “Submitted but Cannot Compile at All”, “Submitted and at least one of them can be compiled”, 第一点, 如果对于某一题是”Not Submitted”的状态的话对应的输出要显示 ‘-’ 表示没有从来没有提交过, 也就是不算分, 不应该输出零分之类的. 第二点, 能不能编译通过这个信息要用来判断要不要输出这个用户, 题目要求是对于那些没有提交过任何可以编译通过的solution的用户, 我们不予输出.

Written on March 7, 2014