PAT-1055. The World's Richest (25)

这道题目就是一个排序题目,但是如果简单的排序会超时,需要剪掉一部分数据。

最多输出100名数据,排序后,那么相同年龄的后面的数据就不会输出了,所以也不需记录在查找序列里面。因此这部分数据可以忽略掉。

bool cmp  return true means right position.

make_heap(iterato_begin,iterator_end);

heap_sort(iterator_begin,iterator_end); 堆排序。

// 1055.cpp : 定义控制台应用程序的入口点。
//

// 1055.cpp : 定义控制台应用程序的入口点。
#include"stdafx.h"
#include<string.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

struct Item
{
    Item(string name,int age,int worth)
    {
        this->name=name;
        this->age=age;
        this->worth=worth;
    }
    bool operator<(const Item & it) const
    {
        if(this->worth>it.worth)
            return true;
        else if(this->worth==it.worth)
        {
            if(this->age<it.age)
                return true;
            else if(this->age==it.age)
            {
                if(this->name<it.name)
                    return true;
            }
        }
        return false;
    }
    string name;
    int age;
    int worth;
};

vector<Item> v;
int agecount[210];
int member[100010];

int main()
{
    int n,k;
    char name[10];
    int value,worth;
    freopen("1055.txt","r",stdin);
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(agecount,0,210);
        for(int i=0;i<n;i++)
        {
            scanf("%s %d %d",name,&value,&worth);
            v.push_back(Item(string(name),value,worth));
        }
        int ind=0;
        for(int i=0;i<n;i++)
        {
            int age=v[i].age;
            if(agecount[age]++<=100)
            {
                member[ind++]=i;
            }

        }
        //make_heap(v.begin(),v.end());
        //sort_heap(v.begin(),v.end());
        sort(v.begin(),v.end());
        int num,b,e;
        for(int i=1;i<=k;i++)
        {
            printf("Case #%d:\n",i);
            scanf("%d%d%d",&num,&b,&e);
            bool flag=false;
            for(int j=0;j<ind;j++)
            {
                int tmp=member[j];
                Item item=v[tmp];
                if(item.age>=b&&item.age<=e)
                {
                    flag=true;
                    printf("%s %d %d\n",item.name.c_str(),item.age,item.worth);
                    num--;
                    if(num<=0)
                        break;
                }
            }
            if(!flag)
                printf("None\n");
        }
    }
    return 0;
}

PAT-1055. The World's Richest (25)

时间: 2024-12-20 13:51:30

PAT-1055. The World's Richest (25)的相关文章

PAT:1055. The World&#39;s Richest (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Person { char name[10]; int age,money; }P[100010]; bool cmp(Person a,Person b) { if(a.money!=b.money) return a.money>b.money; else if(a.age!=b.age) r

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

PAT Advanced 1055 The World&#39;s Richest (25分)

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the n

PAT 1055 The World&#39;s Richest

#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; #define AGE_MAX 200 class People { public: char name[9]; int worth; int age; int idx;

1055. The World&#39;s Richest (25)

1 #include <stdio.h> 2 #include <vector> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 struct MyStruct 8 { 9 char name[9]; 10 int age,worth; 11 }; 12 13 int cmp(MyStruct a,MyStruct b) 14 { 15 if(a.worth!=b.w

1055 The World&#39;s Richest (25 分)

1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain ran

1055 The World&#39;s Richest (25分)

1. 题目 2. 思路 常规题 3. 注意点 注意超时 4. tip 善于使用题目的条件来减少数据量 fill(begin, end, value)注意[begin, end) 2. 代码 #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iostream> // 14:57 - using namespace std; struct p

pat1055. The World&#39;s Richest (25)

1055. The World's Richest (25) 时间限制 400 ms 内存限制 128000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to si

1055. The World&#39;s Richest

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the n