1028. List Sorting (25)

这道题目主要是排序,刚开始简单写了一个代码,发现最后一个测试数据。发现超时了,sort排序用的是快排。快排平均是O(NlogN),最坏是O(N*N)。输入数据是10^5级的,最坏的情况会超过10^10,会超时。所以刚开始想用其他排序方法

sort()---排序

stable_sort---稳定排序

heap_sort()--堆排序(make_heap(a,a+n,cmp1)),heap_sort(a,a+n,cmp1);

最后网上找了一下答案,发现说是输入的原因,所以换成scanf来输入,从char a[20];string str(a);从char a[22]-->string;

发现还是超时,然后把输出也换成printf来输出。最后通过了,cin,cout的效率是比较低,以后还是尽量用scanf,printf吧

// 1028.cpp : 定义控制台应用程序的入口点。
//
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

struct Student
{
    string id;
    string name;
    int score;
}stu[100010];

bool cmp1(const Student & a,const Student &b)
{
    if(a.id<b.id)
        return true;
    return false;
}
bool cmp2(const Student & a,const Student &b)
{
    if(a.name<b.name)
        return true;
    else if(a.name==b.name)
    {
        if(a.id<b.id)
            return true;
    }
    return false;
}
bool cmp3(const Student & a,const Student &b)
{
    if(a.score<b.score)
        return true;
    else if(a.score==b.score)
    {
        if(a.id<b.id)
           return true;
    }
    return false;
}

int main()
{
    int n,c;
    while(cin>>n>>c)
    {
        char id[12];
        char name[12];
        for(int i=0;i<n;i++)
        {
            //cin>>stu[i].id>>stu[i].name>>stu[i].score;
            scanf("%s%s%d",id,name,&stu[i].score);
            stu[i].id=string(id);
            stu[i].name=string(name);
        }

        switch(c)
        {
        case 1:
            make_heap(stu,stu+n,cmp1);
            sort_heap(stu,stu+n,cmp1);
            break;
        case 2:
            make_heap(stu,stu+n,cmp2);
            sort_heap(stu,stu+n,cmp2);
            break;
        case 3:
            make_heap(stu,stu+n,cmp3);
            sort_heap(stu,stu+n,cmp3);
            break;
        }
        for(int i=0;i<n;i++)
        {
            //cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<endl;
            printf("%s %s %d\n",stu[i].id.c_str(),stu[i].name.c_str(),stu[i].score);
        }
    }
    return 0;
}
时间: 2024-08-04 17:39:28

1028. List Sorting (25)的相关文章

PAT 1028. List Sorting (25)

1028. List Sorting (25) 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 th

1028 List Sorting (25 分)

1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wher

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT Advanced 1028 List Sorting (25分)

Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, where N is the number of reco

PAT甲级 1028 List Sorting (25分)(cin cout 超时问题)

注意: 用scanf 和printf 进行输入输出 否则超时 cin,cout速度慢的原因就是它会将数据先读入缓冲区,然后再读入,所以与scanf的直接读入会有点时间差距. 1.换成scanf 和 printf输入输出 2.加一条语句 ios::sync_with_stdio(false); 题目代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algori

PAT:1028. List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char ID[10]; char name[10]; int gread; }STU[100010]; bool cmp1(Student a,Student b) { return strcmp(a.ID,b.ID)<0; } bool cmp2(Student a,Stu

1028. List Sorting

1028. List Sorting (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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, th

pat1028. List Sorting (25)

1028. List Sorting (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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, th

pat1052. Linked List Sorting (25)

1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key a