1055 The World's Richest (25分)

Posted d-i-p

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
", 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
");
    }else{
        for(auto r:p2){
            printf("%s %d %d
", 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分)的主要内容,如果未能解决你的问题,请参考以下文章

1055. The World's Richest (25)

PAT Advanced 1055 The World's Richest (25分)

1055 The World's Richest (25分)

1055 The World's Richest (25分)

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

1055 The World‘s Richest