Skip to content

Instantly share code, notes, and snippets.

@dan0nchik
Created May 10, 2023 08:14
Show Gist options
  • Save dan0nchik/820c510b9a7c6266b94572dbc4d2e891 to your computer and use it in GitHub Desktop.
Save dan0nchik/820c510b9a7c6266b94572dbc4d2e891 to your computer and use it in GitHub Desktop.
'23 HSE Course Contest 5 task 1
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
typedef unsigned int UInt;
struct Date
{
UInt day;
UInt month;
UInt year;
};
struct Applicant
{
std::string name;
std::string surname;
Date birth;
UInt score;
std::vector<std::string> favorite_uni;
};
typedef std::vector<Applicant> ApplicantsVector;
typedef std::map<std::string, UInt> UniCountMap;
typedef std::map<std::string, std::vector<Applicant>> UniApplicantsMap;
bool ApplicantPredicateWithoutScore(const Applicant& lhv, const Applicant& rhv)
{
if (lhv.surname < rhv.surname)
return true;
else if (lhv.surname > rhv.surname)
return false;
if (lhv.name < rhv.name)
return true;
else if (lhv.name > rhv.name)
return false;
if (lhv.birth.year < rhv.birth.year)
return true;
else if (lhv.birth.year > rhv.birth.year)
return false;
if (lhv.birth.month < rhv.birth.month)
return true;
else if (lhv.birth.month > rhv.birth.month)
return false;
if (lhv.birth.day < rhv.birth.day)
return true;
else
return false;
}
bool ApplicantPredicate(const Applicant& lhv, const Applicant& rhv)
{
if (lhv.score > rhv.score)
return true;
else if (lhv.score < rhv.score)
return false;
if (lhv.birth.year < rhv.birth.year)
return true;
else if (lhv.birth.year > rhv.birth.year)
return false;
if (lhv.birth.month < rhv.birth.month)
return true;
else if (lhv.birth.month > rhv.birth.month)
return false;
if (lhv.birth.day < rhv.birth.day)
return true;
else if (lhv.birth.day > rhv.birth.day)
return false;
if (lhv.surname < rhv.surname)
return true;
else if (lhv.surname > rhv.surname)
return false;
if (lhv.name < rhv.name)
return true;
else
return false;
}
void fill_universities(UInt& num_uni, UniCountMap& universities)
{
std::string uni_name;
UInt uni_count;
for (UInt i = 0; i < num_uni; i++)
{
std::cin >> uni_name >> uni_count;
universities[uni_name] = uni_count;
}
}
void fill_favorite_uni(UInt& num_favorites, std::vector<std::string>& favorite_uni)
{
std::string uni;
for (UInt i = 0; i < num_favorites; ++i)
{
std::cin >> uni;
favorite_uni.push_back(uni);
}
}
void fill_applicants(UInt& num_app, ApplicantsVector& applicants)
{
std::string name, surname;
Date date{};
UInt score, num_favorites;
std::vector<std::string> favorite_uni;
for (UInt i = 0; i < num_app; ++i)
{
std::cin >> name >> surname >> date.day >> date.month >> date.year >> score >> num_favorites;
fill_favorite_uni(num_favorites, favorite_uni);
applicants.push_back({name, surname, date, score, favorite_uni});
favorite_uni.clear();
}
}
void sort_applications(ApplicantsVector& applicants)
{
std::sort(applicants.begin(), applicants.end(), ApplicantPredicate);
}
void distribute_applications(ApplicantsVector& applicants, UniCountMap& universities, UniApplicantsMap& results)
{
for (const Applicant& applicant: applicants)
{
for (const std::string& uni: applicant.favorite_uni)
{
if (universities[uni] > 0)
{
results[uni].push_back(applicant);
universities[uni]--;
break;
}
}
}
}
void print_results(UniApplicantsMap& results)
{
for (auto& uni: results)
{
std::cout << uni.first;
std::sort(uni.second.begin(), uni.second.end(), ApplicantPredicateWithoutScore);
for (const Applicant& applicant: uni.second)
{
std::cout << "\t" << applicant.name << " " << applicant.surname;
}
std::cout << std::endl;
}
}
void prefill_results(UniCountMap& universities, UniApplicantsMap& results)
{
for (auto& uni: universities)
{
results[uni.first] = {};
}
}
int main()
{
UInt num_uni, num_app;
UniCountMap universities;
UniApplicantsMap results;
ApplicantsVector applicants;
std::cin >> num_uni;
fill_universities(num_uni, universities);
prefill_results(universities, results);
std::cin >> num_app;
fill_applicants(num_app, applicants);
sort_applications(applicants);
distribute_applications(applicants, universities, results);
print_results(results);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment