1025 PAT Ranking (25分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct node{
	string id; //考号
	int s,fr, loc, lr; //score,final rank,location number ,local rank
}no[30000];
bool cmp(node a, node b) {
	if(a.s!=b.s) return a.s > b.s;
	else return a.id < b.id;
}
int main() {
	int n; cin >> n;
	int c=0;  //总数
	for (int i = 1; i <= n; i++) {
		int start = c;  //起始下标
		int k; cin >> k;
		while (k--) {
			cin >> no[c].id >> no[c].s;
			no[c].loc = i;
			c++;
		}
		sort(no + start, no + c, cmp);  //排序当前考场
		no[start].lr = 1;
		for (int j = start+1; j < c; j++) {
			if (no[j].s == no[j - 1].s) no[j].lr = no[j - 1].lr;
			else no[j].lr = j - start + 1;
		}
	}
	sort(no, no + c, cmp);
	no[0].fr = 1;
	for (int i = 1; i < c; i++) {
		if (no[i].s == no[i - 1].s) no[i].fr = no[i - 1].fr;
		else no[i].fr = i + 1;
	}
	cout << c << endl;
	for (int i = 0; i < c; i++) {
		cout << no[i].id << " " << no[i].fr << " " << no[i].loc << " " << no[i].lr << endl;
	}
}

犯的错误

这里我犯了一个低级错误,导致第四个测试点总是段错误,我还看了半天还不知道错误在哪。还是数组越界的问题,一开始看到N<=100就把结构体数组开了个100的大小,可以N只是考场数,每个考场还有K个学生,因此有N*K个学生,最多可有30000个学生。

发布了26 篇原创文章 · 获赞 5 · 访问量 427

猜你喜欢

转载自blog.csdn.net/weixin_43590232/article/details/104079988
今日推荐