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 <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Testee
{
    char reg_number[15];
    int score;
    int final_rank,location_number,local_rank;
} Testee;

int cmpScore(const void *a,const void *b)
{
    Testee *c = (Testee*)a;
    Testee *d = (Testee*)b;
    if(c->score!=d->score)
        return c->score < d->score ? 1 :-1;
    else
        return strcmp(c->reg_number,d->reg_number);
}

Testee t[300000];

int main()
{
    //5:02
    int i,j;
    int N=0,K=0,total=0;
    //输入N个地区
    scanf("%d",&N);


    for(i=0; i<N; i++)
    {
        //输入N个考生
        scanf("%d",&K);
        //从0开始,0--K1,然后K1--K2....
        for(j=total; j<total+K; j++)
        {
            //输入,并且确定reg_number,location_number;
            scanf("%s %d",(t[j].reg_number),&(t[j].score));
            t[j].location_number = i+1;
        }
        //现在对从这个开始,一直到这个结束的这一块进行排序
        qsort(t+total,total+K,sizeof(Testee),cmpScore);
        //地区第一
        t[total].local_rank = 1;
        //依次后推
        for(j=total+1; j<total+K; j++)
        {
            //计算local_rank;
            //如果这个和前一个成绩不一样,排名就是当前i,一样的话就和上面一个一样;
            if(t[j].score!=t[j-1].score)
                t[j].local_rank = j-total+1;
            else
                t[j].local_rank = t[j-1].local_rank;
        }
        //当前处理完,总数加上这一块
        total += K;
    }
    //输出总数
    printf("%d\n",total);
    //对所有的,不再分地区进行排名
    qsort(t,total,sizeof(Testee),cmpScore);

    
    //计算一下总排名,然后直接输出
    if(total>0)
    {
        t[0].final_rank = 1;
        printf("%s %d %d %d\n",t[0].reg_number,t[0].final_rank,t[0].location_number,t[0].local_rank);

    }
    for(i=1; i<total; i++)
    {
        if(t[i].score!=t[i-1].score)
            t[i].final_rank = i+1;
        else
        {
            t[i].final_rank = t[i-1].final_rank;
        }
        printf("%s %d %d %d\n",t[i].reg_number,t[i].final_rank,t[i].location_number,t[i].local_rank);
    }
    return 0;
}

为什么就是有错呢,就是最后一个测试用例出错,其他的都正确。

思路的话参照算法笔记发现写的没问题啊,自己刚开始想的是用二维数组,但是二维数组不能调用qsort()排序方法,所以说换用了一维数组,根据k来划分,一段一段的处理,然后最后再总起来处理,处理得到的排名放在结构体里直接存上。开始的时候reg_number是用long long存的,没出错,然后因为最后一个错误改成了和《算法笔记》上一样的char数组,但是最后一个还是错了。qsort里的sizeof(Testee)和sizeof(t[0])也都试了,还是没用。还有就是不知道为什么定义数组为[30000]时会出现段错误,[30010]也不行,明明300*100就可以的哇。啊要爆炸了不知道哪里错了。。。