PAT甲级1055 The World‘s Richest:[C++题解]k路归并
生活随笔
收集整理的這篇文章主要介紹了
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路归并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1051 Pop Sequen
- 下一篇: Java API帮助文档怎么查找?