fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. #define int long long
  6. #define oset tree < pair<int, int> , null_type , less<pair<int, int>> , rb_tree_tag , tree_order_statistics_node_update >
  7. using namespace std;
  8. const int N = 1e5;
  9. signed main(){
  10. //We have NAMES not IDs
  11. //We need a data structures that can use strings as a key and int as the value
  12. //Any ideas?
  13. map<string, int> chefvotes;
  14. map<string, int> countryvotes;
  15. map<string, string> country;
  16. int n, m;
  17. cin>>n>>m;
  18. for(int i =0 ;i<n;i++){
  19. string chef, c;
  20. cin>>chef>>c;
  21. country[chef] = c;
  22. }
  23. //We need to store the max for both
  24. string maxcountry;
  25. string maxchef;
  26. int maxcountryvotes = 0;
  27. int maxchefvotes = 0;
  28. for(int i = 0;i<m;i++){
  29. string chef;
  30. cin>>chef;
  31. chefvotes[chef]++;
  32. countryvotes[country[chef]]++;
  33. if(chefvotes[chef]>maxchefvotes){
  34. maxchef = chef;
  35. maxchefvotes = chefvotes[chef];
  36. }
  37. else if(chefvotes[chef]==maxchefvotes){
  38. maxchef = min(chef, maxchef);
  39. }
  40. if(countryvotes[country[chef]]>maxcountryvotes){
  41. maxcountry = country[chef];
  42. maxcountryvotes = countryvotes[country[chef]];
  43. }
  44. else if(countryvotes[country[chef]]==maxcountryvotes){
  45. maxcountry = min(country[chef], maxcountry);
  46. }
  47. }
  48. cout<<maxcountry<<endl;
  49. cout<<maxchef<<endl;
  50. }
  51. //acdabcd
Success #stdin #stdout 0s 4496KB
stdin
Standard input is empty
stdout