1025. PAT Ranking (25)

1025. PAT Ranking (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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


这道题是简单的结构体排序,但在写sort的第三个参数comp,或重载运算符<时遇到了一个问题,strict weak ordering

没有<=,必须严格小于,在相等时返回false,在表达上避开<=。严格意义若排序参见https://blog.csdn.net/tanjinjun2011/article/details/33844357
自己写的程序有一个测试点过不了。

#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct STUDENT
{
	char id[15];
	int score;
	int loc;
	int lr;
	int fn;
	STUDENT() : score(0), loc(0), lr(0), fn(0) {};
	bool operator <(const STUDENT &stu)const
	{
		/*if (this->score < stu.score)return false;
		else if (this->score == stu.score)
		{
			if (strcmp(this->id, stu.id) < 0)return true;
		}
		else return true;*/
		if (this->score != stu.score)return this->score > stu.score;
		else return strcmp(this->id, stu.id) < 0;
	}
};
bool Comp(const STUDENT& a, const STUDENT& b)
{
	if (a.score != b.score)return a.score > b.score;
	else return strcmp(a.id, b.id)<0;
	/*else if (a.score == b.score)
	{
		if (strcmp(a.id, b.id) < 0)return true;
	}*/
	//else return false;
}
STUDENT& Findmax(vector<vector<STUDENT>>& record, vector<int>& head)
{
	int max = 0, index = -1;
	for (int i = 0; i < head.size(); i++)
	{
		if (head[i] < record[i].size()-1)
		{
			if (record[i][head[i]].score > max)
			{
				index = i;
				max = record[i][head[i]].score;
			}
			else if (record[i][head[i]].score == max)
			{
				if (strcmp(record[i][head[i]].id, record[index][head[index]].id) < 0)
				{
					index = i;
					max = record[i][head[i]].score;
				}
			}
		}
		
	}
	head[index]++;
	return record[index][head[index]];

}
int main()
{
	
	int N, M;
	cin >> N;
	vector<vector<STUDENT>>record(N);
	int i,j,sum=0;
	STUDENT stu;
	for (i = 0; i < N; i++)
	{
		cin >> M;
		sum += M;
		for (j = 0; j < M; j++)
		{
			cin >> stu.id >> stu.score;
			stu.loc = i+ 1;
			record[i].push_back(stu);
		}
		sort(record[i].begin(), record[i].end());
	}
	cout << sum << endl;
	for (i = 0; i < N; i++)
	{
		//sort(record[i].begin(), record[i].end(),Comp);
		record[i][0].lr = 1;
		for (j = 1; j < record[i].size(); j++)
		{
			if (record[i][j].score == record[i][j - 1].score)
				record[i][j].lr = record[i][j - 1].lr;
			else
				record[i][j].lr = j+1;	
		}
	}
	vector<STUDENT>rank;
	
	
	for (i = 0; i < N; i++)
	{
		for (j = 0; j < record[i].size(); j++)
			rank.push_back(record[i][j]);
		
	}
	sort(rank.begin(), rank.end());
	int now = 0,score=0;
	
	for (i = 0; i < rank.size(); i++)
	{
		if (rank[i].score != score)
		{
			now = i + 1;
			score = rank[i].score;
		}
		cout << rank[i].id << " " << now<< " " << rank[i].loc << " " << rank[i].lr <<endl;
		
	}
	
	
	return 0;

	
}


贴一个能过的代码

#include<iostream>  
#include<vector>  
#include<set>  
#include<map>  
#include<queue>  
#include<algorithm>  
#include<string>  
#include<string.h>  
using namespace std;  
  
typedef struct Node  
{  
    string registrition_number;  
    int grade;  
    int location_number;  
    int local_rank;  
    int final_rank;  
    bool operator>(const Node& orh) const  
    {  
        if(grade != orh.grade)  
            return grade>orh.grade;  
        else return registrition_number<orh.registrition_number;  
    }  
}Node;  
int main()  
{  
    int n;  
    scanf("%d",&n);  
    vector<Node> globalList;  
    for(int l = 0; l < n; ++l)  
    {  
        int k;  
        scanf("%d",&k);  
        vector<Node> localList(k);  
        for(int i = 0; i < k; ++i)  
        {  
            cin>>localList[i].registrition_number;  
            scanf("%d",&localList[i].grade);  
            localList[i].location_number = l;  
            //globalList.push_back(localList[i]);  
        }  
        //sort  
        sort(localList.begin(), localList.end(), greater<Node>());  
        //process the same grade with the same rank  
        int nowGrade = -1;  
        int nowRank = -1;  
        for(int i = 0; i < k; ++i)  
        {  
            if(localList[i].grade == nowGrade)  
                localList[i].local_rank = nowRank;  
            else  
            {  
                nowGrade = localList[i].grade;  
                nowRank = i;  
                localList[i].local_rank = i;  
            }  
            globalList.push_back(localList[i]);  
        }  
    }  
    //sort the global list  
    //sort  
    sort(globalList.begin(), globalList.end(), greater<Node>());  
    //process the same grade with the same rank  
    int nowGrade = -1;  
    int nowRank = -1;  
    for(int i = 0; i < globalList.size(); ++i)  
    {  
        if(globalList[i].grade == nowGrade)  
            globalList[i].final_rank = nowRank;  
        else  
        {  
            nowGrade = globalList[i].grade;  
            nowRank = i;  
            globalList[i].final_rank = i;  
        }  
    }  
    //output  
    printf("%d\n", globalList.size());  
    for(int i = 0; i < globalList.size(); ++i)  
    {  
        cout<<globalList[i].registrition_number;  
        printf(" %d %d %d\n", globalList[i].final_rank+1, globalList[i].location_number+1, globalList[i].local_rank+1);  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值