zoukankan      html  css  js  c++  java
  • 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:
    ",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
    ",item.name.c_str(),item.age,item.worth);
                        num--;
                        if(num<=0)
                            break;
                    }
                }
                if(!flag)
                    printf("None
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Java的内存区域划分
    Java中的浮点型进行四则运算精度丢失的问题
    单例模式的几种写法
    如何掌握一门工具及对工具的认识
    记一个命运多舛的项目总结
    几个超级好用但很少有人知道的 webstorm技巧
    如何自定义中间件,实现业务代码无侵入监控及拦截
    如何减少和处理死锁
    快照读与加锁读
    谈谈Java常用类库中的设计模式
  • 原文地址:https://www.cnblogs.com/championlai/p/4109268.html
Copyright © 2011-2022 走看看