PAT (Advanced Level) 1055. The World's Richest (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1055. The World's Richest (25)相关的知识,希望对你有一定的参考价值。

排序。随便加点优化就能过。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

struct X
{
    char name[10];
    int age;
    int val;
}s[100000+10];
int n,k,cnt;
int ans[100000+10];

bool cmp(const int &a,const int &b)
{
    if(s[a].val==s[b].val&&s[a].age==s[b].age)
        return strcmp(s[a].name,s[b].name)<0;
    if(s[a].val==s[b].val) return s[a].age<s[b].age;
    return s[a].val>s[b].val;
}

bool CMP(const X&a,const X&b)
{
    if(a.val==b.val&&a.age==b.age)
        return strcmp(a.name,b.name)<0;
    if(a.val==b.val) return a.age<b.age;
    return a.val>b.val;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%s%d%d",s[i].name,&s[i].age,&s[i].val);
    sort(s+1,s+1+n,CMP);
    for(int i=1;i<=k;i++)
    {
        int a,b,c; scanf("%d%d%d",&a,&b,&c);
        cnt=0;
        for(int j=1;j<=n;j++){
            if(s[j].age>=b&&s[j].age<=c) ans[cnt++]=j;
            if(cnt==a) break;
        }
        printf("Case #%d:\n",i);
        if(cnt==0) printf("None\n");
        else
        {
            sort(ans,ans+cnt,cmp);
            for(int j=0;j<min(a,cnt);j++)
                printf("%s %d %d\n",s[ans[j]].name,s[ans[j]].age,s[ans[j]].val);
        }
    }
    return 0;
}

  

以上是关于PAT (Advanced Level) 1055. The World's Richest (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038