MartinPaunov

Lora's_FanClub

May 3rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. // Lora'sFanClub.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <unordered_map>
  8. #include <string>
  9. #include <sstream>
  10. #include <algorithm>
  11.  
  12. std::unordered_map<std::string, std::unordered_map<std::string, int>> boys;
  13.  
  14. void removePossitiveValues(const std::string& name) {
  15.  
  16.     for (auto iter = boys.begin(); iter != boys.end(); iter++) {
  17.         if (iter->first == name) {
  18.             for (auto it = iter->second.begin(); it != iter->second.end(); it++) {
  19.                 if (it->second >= 0) {
  20.                     boys[iter->first][it->first] = 0;
  21.                 }
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. int getTratiValue(const std::string& trait, int value) {
  28.  
  29.     if (trait == "Greedy" || trait == "Rude" || trait == "Dumb") {
  30.         return value * -1;
  31.     }
  32.     else if (trait == "Kind") {
  33.         return value * 2;
  34.     }
  35.     else if (trait == "Handsome") {
  36.         return value * 3;
  37.     }
  38.     else if (trait == "Smart") {
  39.         return value * 5;
  40.     }
  41.     else {
  42.         return value;
  43.     }
  44. }
  45. void setBoys(const std::string& name, const std::string& trait, int value) {
  46.  
  47.     for (auto iter = boys.begin(); iter != boys.end(); iter++) {
  48.         if (iter->first == name) {
  49.             for (auto it = iter->second.begin(); it != iter->second.end(); it++) {
  50.                 if (it->first == trait) {
  51.                     if (it->second < value) {
  52.                         boys[iter->first][it->first] = value;
  53.                         return;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     boys[name][trait] = value;
  60. }
  61. auto cmpBySum = [&](const std::pair<int, std::string>& a, const std::pair<int, std::string>& b)
  62. {
  63.     return a.first != b.first ? a.first > b.first : a.second < b.second;
  64. };
  65.  
  66. auto compByKey = [&](const std::pair<int, std::string>& a, const std::pair<int, std::string>& b) {
  67.  
  68.     return a.first > b.first;
  69. };
  70.  
  71. int main() {
  72.  
  73.     std::string input;
  74.  
  75.     getline(std::cin, input);
  76.  
  77.     while (input != "Make a decision already!") {
  78.  
  79.         std::istringstream arguments(input);
  80.  
  81.         std::string name, trait, value;
  82.         int number;
  83.  
  84.         arguments >> name >> trait >> value;
  85.  
  86.         if (value == "Gyubek!") {
  87.             removePossitiveValues(name);
  88.         }
  89.         else {
  90.             std::istringstream numInput(value);
  91.             numInput >> number;
  92.  
  93.             int traitValue = getTratiValue(trait, number);
  94.  
  95.             setBoys(name, trait, traitValue);
  96.         }
  97.  
  98.         getline(std::cin, input);
  99.     }
  100.     std::vector<std::pair<int, std::string>> sortedBySum;
  101.  
  102.     for (auto iter = boys.begin(); iter != boys.end(); iter++) {
  103.         int sum = 0;
  104.         std::pair<int, std::string> current;
  105.         for (auto it = iter->second.begin(); it != iter->second.end(); it++) {
  106.             sum += it->second;
  107.         }
  108.         current = { sum, iter->first };
  109.         sortedBySum.push_back(current);
  110.     }
  111.  
  112.     std::sort(sortedBySum.begin(), sortedBySum.end(), cmpBySum);
  113.  
  114.     for (auto p : sortedBySum) {
  115.         std::cout << "# "<< p.second << ": " << p.first << std::endl;
  116.         std::vector<std::pair<int, std::string>> sortedByValue;
  117.         for (auto e : boys[p.second]) {
  118.             sortedByValue.push_back(std::pair<int, std::string>{e.second, e.first});
  119.         }
  120.         std::sort(sortedByValue.begin(), sortedByValue.end(), compByKey);
  121.         for (auto x : sortedByValue) {
  122.             if (x.first != 0)
  123.             {
  124.                 std::cout << "!!! " << x.second << " -> " << x.first << std::endl;
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment