1055 The World's Richest (25分)

1. 题目

2. 思路

常规题

3. 注意点

  1. 注意超时

4. tip

  1. 善于使用题目的条件来减少数据量
  2. fill(begin, end, value)注意[begin, end)

2. 代码

#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream> 

// 14:57 -
using namespace std;

struct people{
    string name;
    int age;
    int worth;
    people(string name, int age, int worth){
        this->name = name;
        this->age = age;
        this->worth = worth;
    }
};

int n, k;
vector<people> p[201];
vector<people> pall;

bool cmp(people a, people b){
    if(a.worth != b.worth){
        return a.worth > b.worth;
    }
    if(a.age != b.age){
        return a.age < b.age;
    }
    return a.name < b.name;
}

void print(int m, int amin, int amax, int index){
    printf("Case #%d:\n", index);
    vector<people> p2;
    int pos[201];
    fill(pos+amin, pos+amax+1, 0);
    vector<int> t1;
    for(int i=amin;i<=amax;i++){
        t1.push_back(i);
    }
    vector<int> t2;
    while(1){
        int max = 0x80000000;
        int max_index = -1;
        for(auto i:t1){
            if(p[i].size() != 0){
                if(pos[i] < p[i].size()){
                    t2.push_back(i);
                    if(p[i][pos[i]].worth > max){
                        max = p[i][pos[i]].worth;
                        max_index = i;
                    }
                }
            }
        }
        if(max_index == -1){
            break;
        }else{
            p2.push_back(p[max_index][pos[max_index]]);
            pos[max_index]++;
            if(p2.size() == m){
                break;
            }
        }
        t1 = t2;
        t2.clear();
    }
    if(p2.size() == 0){
        printf("None\n");
    }else{
        for(auto r:p2){
            printf("%s %d %d\n", r.name.data(), r.age, r.worth);
        }
    }
}

int main(){
    int m, amin, amax;
    scanf("%d %d", &n, &k);
    char name[100];
    int age, worth;
    for(int i=0;i<n;i++){
        scanf("%s %d %d", name, &age, &worth);
        pall.push_back(people(name, age, worth));
    }
    sort(pall.begin(), pall.end(), cmp);
    for(int i=0;i<n;i++){
        if(p[pall[i].age].size() < 100){
            p[pall[i].age].push_back(pall[i]);
        }
    }
    for(int i=0;i<k;i++){
        scanf("%d %d %d", &m, &amin, &amax);
        print(m, amin, amax, i+1);
    }
}

1055 The World's Richest (25分)

原文地址:https://www.cnblogs.com/d-i-p/p/12436307.html

时间: 2024-10-08 05:10:18

1055 The World's Richest (25分)的相关文章

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

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 (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

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

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

PAT-1055. The World&#39;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 : 定义控制台应用程

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

5-24 树种统计 (25分)

5-24 树种统计   (25分) 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(\le 10^5≤10?5??),随后N行,每行给出卫星观测到的一棵树的种类名称.种类名称由不超过30个英文字母和空格组成(大小写不区分). 输出格式: 按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位. 输入样例: 29 Red Alder Ash Aspe