日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

PAT甲级1055 The World‘s Richest:[C++题解]k路归并

發布時間:2025/4/5 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级1055 The World‘s Richest:[C++题解]k路归并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 題目分析
      • 題目來源

題目分析



來源:acwing

分析:

采用二維數組vector[N]來存每個年齡的人(結構體),然后分別從大到小排序。剩下的任務就是從給定的年齡[a ,b]中,k路歸并最大的值輸出。

這里k路歸并采用的是for循環選擇最大值。

ac代碼

#include<bits/stdc++.h> using namespace std;const int N =210; int n ,m; struct Person{string name;int age,w;bool operator<(const Person& t)const{if(w != t.w) return w > t.w;if( age != t.age) return age < t.age;return name < t.name;} }; vector<Person> ages[N]; //每個年齡人數 int idx[N]; // 每個年齡段用到第幾個int main(){cin >> n >> m;char name[10];for(int i = 0; i< n ;i++){int age ,w;scanf("%s%d%d" , name ,&age ,& w);ages[age].push_back({name, age ,w});}for(auto & age:ages) sort(age.begin(),age.end());for(int i =1 ; i<= m; i++){printf("Case #%d:\n",i);int cnt,a,b;scanf("%d%d%d",&cnt,&a,&b);memset(idx, 0 ,sizeof idx);bool exist = false;while(cnt--){int t = -1; //表示資產最大的年齡for(int i =a ; i<= b; i++){if(idx[i] < ages[i].size()){//這里小于號已經被重載:資產多的是小于if( t == -1 || ages[i][idx[i]] < ages[t][idx[t]])t = i;}}if ( t == -1) break;auto& p = ages[t][idx[t]];idx[t] ++; //年齡為t的人已經用完1個printf("%s %d %d\n",p.name.c_str(),p.age ,p.w);exist = true;}if(!exist ) cout<<"None"<<endl;}}

題目來源

PAT甲級1055 The World’s Richest

https://www.acwing.com/problem/content/1543/

總結

以上是生活随笔為你收集整理的PAT甲级1055 The World‘s Richest:[C++题解]k路归并的全部內容,希望文章能夠幫你解決所遇到的問題。

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