fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include <sstream>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. struct User {
  12. string name;
  13. int score;
  14. int timestamp; // 上传时间,用于处理同分情况
  15.  
  16. User(string n, int s, int t) : name(n), score(s), timestamp(t) {}
  17.  
  18. // 自定义排序规则:分数降序,同分则时间戳升序(先上传的排前面)
  19. bool operator<(const User& other) const {
  20. if (score != other.score) {
  21. return score > other.score; // 分数高的在前
  22. }
  23. return timestamp < other.timestamp; // 同分,时间早的在前
  24. }
  25. };
  26.  
  27. class RankingSystem {
  28. private:
  29. set<User> users; // 按排名规则自动排序
  30. map<string, pair<int, int>> userInfo; // name -> {score, timestamp}
  31. int currentTime;
  32.  
  33. public:
  34. RankingSystem() : currentTime(0) {}
  35.  
  36. // 上传分数
  37. void upload(const string& name, int score) {
  38. // 如果用户已存在,先删除旧的
  39. auto it = userInfo.find(name);
  40. if (it != userInfo.end()) {
  41. // 从set中删除旧记录
  42. users.erase(User(name, it->second.first, it->second.second));
  43. }
  44.  
  45. // 添加新记录
  46. currentTime++;
  47. userInfo[name] = {score, currentTime};
  48. users.insert(User(name, score, currentTime));
  49. }
  50.  
  51. // 查询指定用户的排名
  52. int queryByName(const string& name) {
  53. auto it = userInfo.find(name);
  54. if (it == userInfo.end()) {
  55. return 0; // 用户不存在
  56. }
  57.  
  58. // 计算排名
  59. User target(name, it->second.first, it->second.second);
  60. int rank = 1;
  61. for (const auto& user : users) {
  62. if (user.name == name) {
  63. return rank;
  64. }
  65. rank++;
  66. }
  67. return 0; // 未找到
  68. }
  69.  
  70. // 查询从指定排名开始的用户
  71. vector<string> queryByRank(int rank) {
  72. vector<string> result;
  73. if (rank < 1 || rank > users.size()) {
  74. return result;
  75. }
  76.  
  77. int count = 0;
  78. int currentRank = 1;
  79. for (const auto& user : users) {
  80. if (currentRank >= rank && count < 10) {
  81. result.push_back(user.name);
  82. count++;
  83. }
  84. currentRank++;
  85. if (count >= 10) break;
  86. }
  87.  
  88. return result;
  89. }
  90.  
  91. // 解析并处理命令
  92. void processCommand(const string& command) {
  93. size_t colonPos = command.find(':');
  94. if (colonPos == string::npos) {
  95. return;
  96. }
  97.  
  98. string cmd = command.substr(0, colonPos);
  99. string argsStr = command.substr(colonPos + 1);
  100.  
  101. if (cmd == "Upload") {
  102. // 格式: Upload:A 20
  103. size_t spacePos = argsStr.find(' ');
  104. if (spacePos != string::npos) {
  105. string name = argsStr.substr(0, spacePos);
  106. int score = stoi(argsStr.substr(spacePos + 1));
  107. upload(name, score);
  108. }
  109. } else if (cmd == "QueryByName") {
  110. // 格式: QueryByName:A
  111. string name = argsStr;
  112. cout << queryByName(name) << endl;
  113. } else if (cmd == "QueryByRank") {
  114. // 格式: QueryByRank:2
  115. int rank = stoi(argsStr);
  116. vector<string> names = queryByRank(rank);
  117. for (size_t i = 0; i < names.size(); i++) {
  118. if (i > 0) cout << " ";
  119. cout << names[i];
  120. }
  121. if (!names.empty()) {
  122. cout << endl;
  123. }
  124. }
  125. }
  126. };
  127.  
  128. int main() {
  129. ios::sync_with_stdio(false);
  130. cin.tie(nullptr);
  131.  
  132. int N;
  133. cin >> N;
  134. cin.ignore(); // 忽略换行符
  135.  
  136. RankingSystem system;
  137.  
  138. for (int i = 0; i < N; i++) {
  139. string command;
  140. getline(cin, command);
  141. system.processCommand(command);
  142. }
  143.  
  144. return 0;
  145. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty