Categories
不学无术

1028. List Sorting (25) | STL中自定义排序

时间限制

200 ms
内存限制
32000 kB
代码长度限制
16000 B
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input
Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
Output
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.
Sample Input 1

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90

Sample Output 3

000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

============================================
这个题目能想到最好的办法,就是用set然后重载一个自定义排序了 感谢郭嘉,感谢STL!
set这个东西嘛,好像是用红黑树还是什么实现的(印象中是这样的) 原来打算用vector的,但是vector好像不能在插入的时候就根据自定义的排序方法排序好插进去,这样如果全部录入完了整体排序一遍是不是更耗费时间呢?
参考文章:
http://blog.csdn.net/stone_sky/article/details/8471722 讲的是自定义排序 http://zhidao.baidu.com/question/189798009.html 讲的是输出填0格式化问题 http://www.189works.com/article-43335-1.html http://wenku.baidu.com/view/08c0eb0bff00bed5b9f31d29.html 这两个就是自定义排序了
这题目这么搞了以后就简单多了:)当然最后一个测试点还是用了100ms…不管了过了就行 ============================================

#include
#include
#include
#include
using namespace std;
int compareName(const char name1[],const  char name2[])
{
    for(int i=0; i<8; i++)
    {
        if( name1[i] < name2[i] )
            return 1;
        else if(name1[i] > name2[i])
            return -1;
        //相等的话继续比较
    }
    return 0; //相同的名字
}
struct StudentRecord
{
    int ID;
    char name[9];
    int grade;
    int sortType; //按哪一列排序,1,2,3
    StudentRecord(){}
    StudentRecord(int _id, char _name[], int _grade, int _sortType)
    {
        ID = _id;
        strcpy(name, _name);
        grade = _grade;
        sortType = _sortType;
    }
    void set(int _id, char _name[], int _grade)
    {
        ID = _id;
        strcpy(name, _name);
        grade = _grade;
    }
    //自定义排序方法
    bool operator< (const StudentRecord& other) const
    {
        int cmpResult;
        switch(sortType)
        {
        case 1:
            //按照ID升序排列
            return ID < other.ID;
        case 2:
            //按照姓名的升序排列,如果有同名再按ID升序
            cmpResult = compareName(name, other.name);
            if(cmpResult > 0)
                return true;
            else if (cmpResult < 0)
                return false;
            else
                return ID < other.ID;
        case 3:
            //按照成绩的升序排列,如果有同名成绩按ID升序
            if (grade == other.grade)
                return ID < other.ID;
            else
                return grade < other.grade;
        default:
            printf("ERROR!");
            return false;
        }
    }
};
set records;
int main()
{
    long N; int C;
    scanf("%ld %d", &N, &C);
    StudentRecord singleRec;
    singleRec.sortType = C;
    for(long i=0; i<N; i++)
    {
        int ID, grade;
        char name[8];
        scanf("%d %s %d", &ID, name, &grade);
        singleRec.set(ID, name, grade);
        records.insert(singleRec);
    }
    set::iterator it;
    for(it = records.begin(); it != records.end(); it++)
    {
        StudentRecord sr = *it;
        printf("%06d %s %dn", sr.ID, sr.name, sr.grade);
    }
    return 0;
}

====================================

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 790 5/5
1 答案正确 0 740 5/5
2 答案正确 0 750 5/5
3 答案正确 0 790 2/2
4 答案正确 0 790 2/2
5 答案正确 0 740 2/2
6 答案正确 100 8970 4/4

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.