1055.The World’s Richest (25)
pat-al-1055
2017-02-08
一開始思路是這樣的:先按年齡排序,然后對每一查詢,找到起始和結束的下標,將這一段按題意排序,依次輸出,其中有些對于None情況的特殊判斷,然后進行一次排序將數組還原。有一個測試點超時。查了查,發現有些說暴力排序會超時,要換方法,但是查到這個:solution Of 1055. The World’s Richest (25)所以說直接查詢就好了,是我(們)想復雜了。/** * pat-al-1055 * 2017-2-08 * Cpp version * Author: fengLian_s */#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct WLTH{ char name[10]; int age; int worth;}wlth[100002];bool cmp(WLTH a, WLTH b)//按財富非升序排序,財富相同按年齡非降序排序,前兩者都相同,按姓名非降序排序{ if(a.worth != b.worth) return a.worth > b.worth; else { if(a.age != b.age) return a.age < b.age; else return strcmp(a.name, b.name) < 0; }}int main(){ freopen("in.txt", "r", stdin); int n, k; scanf("%d%d", &n, &k); for(int i = 0;i < n;i++) { scanf("%s%d%d", wlth[i].name, &wlth[i].age, &wlth[i].worth); } sort(wlth, wlth+n, cmp); // for(int i = 0;i < n;i++) // { // -FIN-新聞熱點
疑難解答