Problem Solution

1 solutions

  • 1
    @ 2024-12-12 18:53:08

    Problem Setter : Araf Al Jami (CLown1331)
    Problem tag : Implementation
    Tutorial : Use a priority queue to maintain the list of candidates following the priority logic defined in the description.
    Time complexity : O(N log N)
    Code :

    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <map>
    #include <queue>
    #include <cassert>
    using namespace std;
    
    const int total_people_limit = 5e5;
    const int number_of_days_limit = 1e4;
    const int number_of_priority_jobs_limit = 150;
    const int number_of_age_groups_limit = 7;
    
    map <string, int> job_priorities;
    int ages[126];
    struct people {
        int id;
        int age;
        string name;
        string job;
        int priority;
        people(int _id, int _age, string _name, string _job) {
            assert(0 < _id);
            assert(0 < _age && _age <= 125);
            this->id = _id;
            this->age = _age;
            this->name = _name;
            this->job = _job;
            this->priority = job_priorities[this->job] + ages[this->age];
        }
        bool operator < (const people &rhs) const {
            if (this->priority == rhs.priority) {
                return this->id > rhs.id;
            }
            return this->priority < rhs.priority;
        }
    };
    
    priority_queue <people> pq;
    
    int main() {
        int total_people = 0;
        int number_of_days;
        int number_of_priority_jobs;
        int number_of_age_groups;
        string job, name;
        int job_priority, id, age;
        scanf("%d %d %d", &number_of_days, &number_of_priority_jobs, &number_of_age_groups);
        for (int i = 0; i < number_of_priority_jobs; i++) {
            cin >> job >> job_priority;
            assert(job_priority <= number_of_priority_jobs);
            job_priorities[job] = job_priority;
        }
        for (int i = 0; i < number_of_age_groups; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            assert(x <= y);
            assert(0 < x && y <= 125);
            assert(z <= number_of_age_groups);
            for (int j = x; j <= y; j++) {
                ages[j] = z;
            }
        }
        id = 0;
        long long vaccine_available = 0;
        for (int i = 0 ; i < number_of_days; i++) {
            int n, m;
            cin >> n >> m;
            total_people += n;
            vaccine_available += m;
            assert(n > 0);
            assert(m >= 0);
            // cerr << "day " << (i + 1) << ":\n";
            for (int j = 0; j < n; j++) {
                cin >> name >> job >> age;
                assert(0 < age && age <= 125);
                pq.push(people(++id, age, name, job));
            }
            while (vaccine_available > 0 && pq.size() > 0)
            {
                people it = pq.top();
                cout << it.name << " " << it.job << " " << it.age;
                // cerr << " " << it.priority << " " << it.id;
                cout << "\n";
                pq.pop();
                vaccine_available--;
            }
        }
        assert(total_people <= total_people_limit);
        cout << "Still unvaccinated people: " << pq.size() << "\n";
    }
    
    
  • 1

Information

ID
1148
Difficulty
6
Category
(None)
Tags
(None)
# Submissions
59
Accepted
17
Accepted Ratio
29%
Uploaded By