zoukankan      html  css  js  c++  java
  • PAT甲级1055 The World's Richest【排序】

    题目https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768

    题意:

    给定n个人的名字,年龄和身价。k次查询,每次询问某一个年龄区间的人的前m个最富有的人。

    思路:

    我好傻系列。

    刚开始撒比排序先按照年龄从小到大排然后存某一年龄的开始下标和个数。然后每次复制出某一区间的人,再按答案要求排序。

    好傻。后来想想直接就按照答案的要求排序,对于符合要求的那些人,他们输出的时候的相对顺序就是固定的。

    所以我只需要从头到尾找到前m个符合要求的人就可以了。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<map>
     4 #include<set>
     5 #include<iostream>
     6 #include<cstring>
     7 #include<algorithm>
     8 #include<vector>
     9 #include<cmath> 
    10 #include<stack>
    11 #include<queue>
    12 
    13 #define inf 0x7fffffff
    14 using namespace std;
    15 typedef long long LL;
    16 typedef pair<int, int> pr;
    17 
    18 int n, k;
    19 const int maxn = 1e5 + 5;
    20 struct node{
    21     string name;
    22     int age;
    23     int net_worth;
    24 }peo[maxn], tmp[maxn];
    25 
    26 
    27 bool cmp1(node a, node b)
    28 {
    29     if(a.age == b.age)return a.net_worth > b.net_worth;
    30     else return a.age < b.age;
    31 }
    32 
    33 bool cmp(node a, node b)
    34 {
    35     if(a.net_worth == b.net_worth){
    36         if(a.age == b.age)return a.name < b.name;
    37         else return a.age < b.age;
    38     }
    39     else return a.net_worth > b.net_worth; 
    40 }
    41 
    42 int main()
    43 {
    44     scanf("%d%d", &n, &k);
    45     for(int i = 1; i <= n; i++){
    46         cin>>peo[i].name>>peo[i].age>>peo[i].net_worth;
    47     }
    48     sort(peo + 1, peo + 1 + n, cmp);
    49     
    50     int m, amin, amax;
    51     for(int cas = 1; cas <= k; cas++){
    52         scanf("%d%d%d", &m, &amin, &amax);
    53         printf("Case #%d:
    ", cas);
    54         
    55         int cnt = 0;
    56         for(int pos = 1; pos <= n; pos++){
    57             if(peo[pos].age >= amin && peo[pos].age <= amax){
    58                 cnt++;
    59                 cout<<peo[pos].name;
    60                 printf(" %d %d
    ", peo[pos].age, peo[pos].net_worth);
    61             }
    62             if(cnt == m)break;
    63         }
    64         if(cnt == 0){
    65             printf("None
    ");
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    CSS3-给网页添加图片
    CSS3-margin,padding,border
    布局左固定右自适应
    Java-基础编程(螺旋矩阵&乘法表)
    Java IO流整理Rick
    Java-Eclipse插件开发学习笔记
    关于《程序语言-平台优越性》一文补充说明
    程序语言-平台优越性
    Understand RNN with TensorFlow in 7 Steps
    pandas mean 返回 inf
  • 原文地址:https://www.cnblogs.com/wyboooo/p/10431627.html
Copyright © 2011-2022 走看看