2A - Winner
2013-10-14 21:48 BrokenJar 阅读(152) 评论(0) 编辑 收藏 举报#include <iostream> #include <vector> #include <string> #include <algorithm> #include <functional> class player{ public: player(const std::string& name) :_name(name), _score(0) { } void addscore(int score) { _score += score; _count ++; _scores.push_back(_score); _counts.push_back(_count); } inline const std::string& name() { return _name; } int reach() { std::vector<int>::iterator pos; pos = std::find_if(_scores.begin(), _scores.end(), std::not1(std::bind2nd(std::less<int>(), _score))); std::vector<int>::difference_type dis; dis = std::distance(_scores.begin(), pos); return _counts[dis]; } const bool operator < (player __x) { return ((_score < __x._score) || ((_score == __x._score) && (reach() > __x.reach()))); } const bool operator == (const player& __x) { return _name == __x._name; } private: std::vector<int> _scores; std::vector<int> _counts; std::string _name; int _score; static int _count; }; int player::_count = 0; int main() { int n; std::vector<player> players; std::cin >> n; for(int i = 0; i < n; ++i) { std::string name; int score; std::cin >> name >> score; std::vector<player>::iterator iter; iter = std::find(players.begin(), players.end(), player(name)); if(iter == players.end()) { players.push_back(player(name)); players.back().addscore(score); } else iter->addscore(score); } std::vector<player>::iterator winner; winner = std::max_element(players.begin(), players.end()); std::cout << winner->name() << std::endl; }