fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include<algorithm>
  8.  
  9. class Score {
  10. private:
  11. std::string country;
  12. int score;
  13. public:
  14. Score(std::string c, int s) {
  15. country = c;
  16. score = s;
  17. //std::cout << c << "," << s << std::endl;
  18. }
  19.  
  20. std::string getCountry() {return country;}
  21. void unsetCountry() {country = "";}
  22. int getScore() {return score;}
  23. };
  24.  
  25. /**
  26.  * Read `country: score` pairs from infile and return a list of pairs
  27.  * Stop when `infile` reaches EOF
  28.  */
  29. bool cmp(Score &s1, Score &s2)
  30. {
  31. if(s1.getScore() > s2.getScore())
  32. {
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38. void readScores(std::istream &infile, std::vector<Score> &scores) {
  39. while(infile) {
  40. std::string country;
  41. infile >> std::ws;
  42. std::getline(infile, country, ':');
  43. int runs;
  44. infile >> runs;
  45. if (country != "") {
  46. Score score(country, runs);
  47. scores.push_back(score);
  48. }
  49. }
  50. }
  51.  
  52. int noCenturyCountryCount(std::vector<Score> &scores) {
  53. int noCenturyCount = 0;
  54. sort(scores.begin(),scores.end(),cmp);
  55. //for(auto s: scores)
  56. //{
  57. // std::cout<<s.getCountry()<<" "<<s.getScore()<<std::endl;
  58. //}
  59. for(int i=0; i<scores.size(); i++) {
  60. const std::string &country = scores[i].getCountry();
  61.  
  62. int centuries = 0;
  63. for(int j=i; j<scores.size(); j++) {
  64. if(scores[j].getCountry() == "")
  65. {
  66. continue;
  67. }
  68. if (scores[j].getCountry() == country) {
  69. if (scores[j].getScore() >= 100) {
  70. centuries++;
  71. }
  72. // disable checking of this same country again
  73.  
  74.  
  75. //scores[j].unsetCountry();
  76.  
  77. for(auto it=scores.begin();it!=scores.end();it++)
  78. {
  79. //std::cout<<(*it).getScore()<<std::endl;
  80. if((*it).getCountry() == country)
  81. {
  82. scores.erase(it);
  83. it--;
  84. }
  85. }
  86. //std::cout<<scores.size()<<std::endl;
  87. std::cout<<centuries<<std::endl;
  88. }
  89. if (centuries == 0) {
  90. noCenturyCount++;
  91. }
  92. }
  93. }
  94. return noCenturyCount;
  95. }
  96.  
  97. int main (void) {
  98. std::vector<Score> scores;
  99. readScores(std::cin, scores);
  100. int noCenturyCount = noCenturyCountryCount(scores);
  101. std::cout << noCenturyCount << std::endl;
  102. }
Success #stdin #stdout 0s 5484KB
stdin
India: 3
Australia: 31
India: 122
stdout
1
0