1. 程式人生 > 實用技巧 >pat 1025

pat 1025

1025PAT 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 (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), 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 toN. 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

題意:給定n個考場的pat考生成績,要求按所有考試的排名輸出,並且要求輸出考生所屬考場以及所屬考場排名。

思路:用一個vector儲存所有考生成績、id,對這個vector用sort函式排序,得到所有考生的分數排序。

   用n個vector儲存每個考場的考生成績,id,排序,得到每個考場所有考生的分數排序。

   題目要求同分數的排名應該相同,因此node結點裡面設定兩個變數:rank表示總排名,local_rank表示所屬考場排名。對相同成績的考生進行處理,使其(local_)rank相同,如何處理參考第二個和第三個for迴圈。

程式碼如下:

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
struct node{
    string id;
    int socre;
    int local_num;
    int rank;
    int local_rank;
};
bool cmp(node a,node b){
    if(a.socre!=b.socre)
        return a.socre>b.socre;
    else
        return a.id<b.id;
}
vector<node> stu;
vector<node> stu_local[105]; 
int main(){
    int n,k;
    scanf("%d",&n);
    int socre;
    string id;
    for(int i=0;i<n;i++){
        scanf("%d",&k);
        for(int j=0;j<k;j++){
            cin>>id>>socre;
            node temp;
            temp.id=id;
            temp.local_num=i+1;
            temp.socre=socre;
            stu.push_back(temp);
            stu_local[i].push_back(temp);
        }
        sort(stu_local[i].begin(),stu_local[i].end(),cmp);    
    }
    sort(stu.begin(),stu.end(),cmp);
    int rank=1;
    for(int i=0;i<stu.size();i++){
        int j=i;
        stu[j].rank=rank;
        while(i+1<stu.size()&&stu[i+1].socre==stu[j].socre){
            
            stu[i+1].rank=rank;
            i++;
        }
        rank=rank+i-j+1;
    }
    map<string,int> m;
    for(int i=0;i<n;i++){
        int local_rank=1;
        
        for(int j=0;j<stu_local[i].size();j++){
            int k=j;
            stu_local[i][k].local_rank=local_rank;
            m[stu_local[i][k].id]=local_rank;
            while(j+1<stu_local[i].size()&&stu_local[i][j+1].socre==stu_local[i][k].socre){
                
                stu_local[i][j+1].local_rank=local_rank;
                m[stu_local[i][j+1].id]=local_rank;
                j++;
            }
            local_rank=local_rank+j-k+1;
        }
    }
    printf("%d\n",stu.size());
    for(int i=0;i<stu.size();i++){
        printf("%s %d %d %d\n",stu[i].id.c_str(),stu[i].rank,stu[i].local_num,m[stu[i].id]);
    }
    return 0;
}