日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

1028. List Sorting (25)

發布時間:2025/3/20 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }

?

轉載于:https://www.cnblogs.com/championlai/p/4014652.html

總結

以上是生活随笔為你收集整理的1028. List Sorting (25)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。