This topic is mainly sorted. At the beginning, I wrote a simple code and found the last test data. The time-out is found, and the sort sorting uses the fast sorting. The average speed is O (nlogn), and the worst is O (n * n ). The input data is 10 ^ 5, and the worst case will exceed 10 ^ 10, which will time out. So I want to use other sorting methods at the beginning.
Sort () --- sort
Stable_sort --- stable sorting
Heap_sort () -- heap sorting (make_heap (A, A + N, CMP1), heap_sort (A, A + N, CMP1 );
Finally, I found the answer on the Internet and found it was the reason for the input. Therefore, I changed it to scanf for input, from Char A [20]; string STR (); from char a [22] --> string;
Find or time out, and change the output to printf. The final result is: the efficiency of CIN and cout is relatively low. In the future, try to use scanf and 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;}
1028. List sorting (25)