2A Codeforces
unknown
c_cpp
3 years ago
3.9 kB
4
Indexable
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main() {
// n to keep the number of players
int n = 0,
// max to remember the maximum amount of points
max = 0,
// score to input scores
score = 0,
// nrOfMax to keep track of how many players with a maximum amount of points there are
nrOfMax = 0;
// name to input playernames
string name;
// names to keep playernames
vector<string> names;
// currentScores to keep track of player points at any given moment of time
map<string, int> currentScores;
// nameScore to map a player's total score to its name
map<string, int> nameScore;
// scores to store playerscores
vector<int> scores;
// nameHidden turns to false if the winner has been chosen
bool nameHidden = true;
// topNames to remember the players who have the max amount of points, in case there's more than one
map<string, int> topNames;
// Reads number of rounds
cin >> n;
for (int i = 0; i != n; i++) {
cin >> name >> score;
// Registers all the players and scores
scores.push_back(score);
names.push_back(name);
// If the name isn't in the nameScore vector, register it.
if (nameScore.count(name) == 0) {
// Filling the currentScores vector with playernames, with 0 as their current score right now
currentScores.insert(pair<string, int>(name, 0));
nameScore.insert(pair<string, int>(name, score));
}
// If the name is already in the nameScore vector, sum their points
// to find out the total number of points per player
else {
nameScore[name] += score;
}
}
// Finds out the maximum number of points
for (auto it = nameScore.begin(); it != nameScore.end(); ++it) {
if (it->second > max) max = it->second;
}
// Finds out the number of players with maximum points
for (auto it = nameScore.begin(); it != nameScore.end(); ++it) {
if (it->second == max) nrOfMax++;
}
// If there's just one player with a maximum amount of points, he's the winner
for (auto it = nameScore.begin(); it != nameScore.end(); ++it) {
if (nrOfMax == 1) {
if (it->second == max) cout << it->first;
}
// If there are more than one maximum-point players
else {
// Populate the vector with the names of all the maximum-point players
for (auto it = nameScore.begin(); it != nameScore.end(); ++it) {
if (it->second == max) topNames.insert(pair<string, int>(it->first, 0));
}
// Iterating through the length of the vector containing the inputs,
// calculate the current points of the player with the i position on the vector.
// The first player to get the points which equal to maximum is the winner.
for (int i = 0; i != names.size(); i++) {
// If the name at the i vector position is registered in topNames, calculate
// their current score at this given moment
if (topNames.count(names.at(i)) > 0)
currentScores[names.at(i)] += scores.at(i);
// nameHidden bool parameter to keep track if the winner has been found or not
// If the current score of the player with the i vector position is equal or more than
// the max amount of points per player, he is the winner and nameHidden turns false,
// thus ending the program.
if (currentScores[names.at(i)] >= max && nameHidden == true) {
cout << names.at(i);
nameHidden = false;
break;
}
}
}
}
}Editor is loading...