fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <algorithm>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. struct User {
  11. string name;
  12. int score;
  13. int timestamp; // 上传时间,用于处理同分情况
  14. };
  15.  
  16. class RankingSystem {
  17. private:
  18. vector<User> users; // 按排名排序的用户列表
  19. unordered_map<string, User*> userMap; // 用户名到用户指针的映射
  20. int currentTime; // 当前时间戳
  21.  
  22. // 比较函数:按分数降序,分数相同时按时间戳升序(先上传的靠前)
  23. static bool compareUsers(const User& a, const User& b) {
  24. if (a.score != b.score) {
  25. return a.score > b.score;
  26. }
  27. return a.timestamp < b.timestamp;
  28. }
  29.  
  30. // 重新排序用户
  31. void sortUsers() {
  32. sort(users.begin(), users.end(), compareUsers);
  33. }
  34.  
  35. public:
  36. RankingSystem() : currentTime(0) {}
  37.  
  38. // Upload操作
  39. void upload(const string& name, int score) {
  40. // 如果用户已存在,删除旧记录
  41. if (userMap.find(name) != userMap.end()) {
  42. // 从vector中删除旧记录
  43. for (auto it = users.begin(); it != users.end(); ++it) {
  44. if (it->name == name) {
  45. users.erase(it);
  46. break;
  47. }
  48. }
  49. }
  50.  
  51. // 创建新用户记录
  52. User newUser;
  53. newUser.name = name;
  54. newUser.score = score;
  55. newUser.timestamp = currentTime++;
  56.  
  57. // 添加到vector和map
  58. users.push_back(newUser);
  59. userMap[name] = &users.back();
  60.  
  61. // 重新排序
  62. sortUsers();
  63. }
  64.  
  65. // QueryByName操作
  66. int queryByName(const string& name) {
  67. if (userMap.find(name) == userMap.end()) {
  68. return -1; // 用户不存在
  69. }
  70.  
  71. // 查找用户排名(排名从1开始)
  72. for (size_t i = 0; i < users.size(); ++i) {
  73. if (users[i].name == name) {
  74. return i + 1;
  75. }
  76. }
  77. return -1;
  78. }
  79.  
  80. // QueryByRank操作
  81. vector<string> queryByRank(int rank) {
  82. vector<string> result;
  83. // rank是1-based的,转换为0-based索引
  84. int startIdx = rank - 1;
  85.  
  86. // 确保起始索引有效
  87. if (startIdx < 0 || startIdx >= (int)users.size()) {
  88. return result;
  89. }
  90.  
  91. // 获取最多10个用户
  92. for (int i = startIdx; i < (int)users.size() && i < startIdx + 10; ++i) {
  93. result.push_back(users[i].name);
  94. }
  95.  
  96. return result;
  97. }
  98. };
  99.  
  100. int main() {
  101. ios::sync_with_stdio(false);
  102. cin.tie(nullptr);
  103.  
  104. int N;
  105. cin >> N;
  106. cin.ignore(); // 忽略换行符
  107.  
  108. RankingSystem system;
  109.  
  110. for (int i = 0; i < N; ++i) {
  111. string line;
  112. getline(cin, line);
  113.  
  114. // 解析操作类型
  115. size_t colonPos = line.find(':');
  116. if (colonPos == string::npos) {
  117. continue; // 无效输入
  118. }
  119.  
  120. string operation = line.substr(0, colonPos);
  121. string args = line.substr(colonPos + 1);
  122.  
  123. if (operation == "Upload") {
  124. // 解析用户名和分数
  125. size_t spacePos = args.find(' ');
  126. if (spacePos == string::npos) {
  127. continue; // 无效输入
  128. }
  129.  
  130. string name = args.substr(0, spacePos);
  131. int score = stoi(args.substr(spacePos + 1));
  132.  
  133. system.upload(name, score);
  134. }
  135. else if (operation == "QueryByName") {
  136. string name = args;
  137. int rank = system.queryByName(name);
  138. cout << rank << endl;
  139. }
  140. else if (operation == "QueryByRank") {
  141. int rank = stoi(args);
  142. vector<string> result = system.queryByRank(rank);
  143.  
  144. if (!result.empty()) {
  145. for (size_t j = 0; j < result.size(); ++j) {
  146. cout << result[j];
  147. if (j < result.size() - 1) {
  148. cout << " ";
  149. }
  150. }
  151. }
  152. cout << endl;
  153. }
  154. }
  155.  
  156. return 0;
  157. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty