数据结构 10-排序5 PAT Judge(C++)

数据结构 10-排序5 PAT Judge(C++)_第1张图片

 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 -

code

# include 
# include 
# include 
# include 

int max(int a, int b) { return a > b ? a : b; }

struct user
{
	int id;
	int Q[6];
	int total_score;
	int perfect_solve;
	int rank;
	bool rankIsValid;
	void init(int i)
	{
		id = i;
		rankIsValid = false;
		Q[1] = Q[2] = Q[3] = Q[4] = Q[5] = -1; //-1表示从未提交过
		total_score = perfect_solve = 0;
	}

	void statistics(int * perfectScore, int totalQ)
	{
		for (int i = 1; i <= totalQ; ++i)
		{
			total_score += Q[i] == -1 ? 0 : Q[i];
			if (Q[i] == perfectScore[i]) perfect_solve++;
		}
	}

	void countRank(int r, user const & u)
	{
		if (total_score == u.total_score) rank = u.rank;
		else rank = r;
	}

	void print(int totalQ)
	{
		if (!rankIsValid) return;
		printf("%d %05d %d", rank, id, total_score);
		for (int i = 1; i <= totalQ; ++i)
		{
			if (Q[i] == -1) printf(" -");
			else printf(" %d", Q[i]);
		}
		printf("\n");
	}

	void update(int qid, int score)
	{
		if (score != -1) rankIsValid = true;

		if (Q[qid] == -1) Q[qid] = 0;
		Q[qid] = max(Q[qid], score);
	}
};

bool cmp(user const & a, user const & b)
{
	if (a.total_score != b.total_score) return a.total_score > b.total_score;
	else if (a.perfect_solve != b.perfect_solve) return a.perfect_solve > b.perfect_solve;
	else return a.id < b.id;
}


int main(void)
{
	int n, totalQ, k;

	int perfectScore[6];
	scanf("%d%d%d", &n, &totalQ, &k);
	
	for (int i = 1; i <= totalQ; ++i) scanf("%d", &perfectScore[i]);

	user * U = new user[n + 1];
	for (int i = 1; i <= n; ++i) U[i].init(i);

	while (k--)
	{
		int id, qid, score;
		scanf("%d%d%d", &id, &qid , &score);
		U[id].update(qid, score);
	}
	for (int i = 1; i <= n; ++i) U[i].statistics(perfectScore, totalQ);

	// 排序
	std::sort(U+1, U + n + 1, cmp);

	// 给排名
	for (int i = 1; i <= n; ++i) U[i].countRank(i, U[i - 1]);

	//输出
	for (int i = 1; i <= n; ++i)
		U[i].print(totalQ);

	return 0;
}

 

你可能感兴趣的:(ZJU数据结构,数据结构,c++,算法)