• 822 - Queue and A (UVA)


    题目链接如下:

    Online Judge

    这又是一道很恶心的模拟题。(然后看到网上说“又是一道简单的流程模拟题”,差点昏过去……)

    开始的代码超时,想不出好的解决办法。后来看到uva 822_uva822 题意-CSDN博客 里一句“以人为循环主体来找工作,按照题目的要求(两个要求, 自己定义一个比较函数)把存储人的数组先进行排序, 对排序后的人依次找一个topic做就行了”,点醒了我。人只需要排序一次即可,每次按这个顺序让客服挑工作。

    AC代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. // #define debug
    7. struct topic{
    8. int nbrOfRequest, elapsedTime, serviceTime, timeGap, lastRequest;
    9. };
    10. struct person{
    11. int nbrOfTopics, idRank, recentJob;
    12. int finishTime = 0;
    13. std::vector<int> topicPriority;
    14. };
    15. int n, tid, nbrOfRequest, elapsedTime, serviceTime, timeGap, pid, nbrOfTopics, priority, lastRequest, finishTime, curr, kase = 0;
    16. std::unordered_map<int, topic> topicMap;
    17. std::unordered_map<int, person> personMap;
    18. std::unordered_map<int, int> currRequest;
    19. std::vector<int> pidVec;
    20. std::set<int> ttime;
    21. bool cmp(const int &a, const int &b){
    22. return personMap[a].recentJob != personMap[b].recentJob ? personMap[a].recentJob < personMap[b].recentJob : personMap[a].idRank < personMap[b].idRank;
    23. }
    24. int main(){
    25. #ifdef debug
    26. freopen("0.txt", "r", stdin);
    27. freopen("1.txt", "w", stdout);
    28. #endif
    29. while (scanf("%d", &n) == 1 && n){
    30. lastRequest = 0;
    31. finishTime = 0;
    32. ttime.clear();
    33. ttime.insert(0);
    34. pidVec.clear();
    35. topicMap.clear();
    36. personMap.clear();
    37. for (int i = 0; i < n; ++i){
    38. scanf("%d %d %d %d %d", &tid, &nbrOfRequest, &elapsedTime, &serviceTime, &timeGap);
    39. topicMap[tid].nbrOfRequest = nbrOfRequest;
    40. topicMap[tid].elapsedTime = elapsedTime;
    41. topicMap[tid].serviceTime = serviceTime;
    42. topicMap[tid].timeGap = timeGap;
    43. topicMap[tid].lastRequest = elapsedTime + (nbrOfRequest - 1) * timeGap;
    44. lastRequest = std::max(lastRequest, topicMap[tid].lastRequest);
    45. for (int j = 0; j < nbrOfRequest; ++j){
    46. ttime.insert(elapsedTime + j * timeGap);
    47. }
    48. }
    49. scanf("%d", &n);
    50. for (int i = 0; i < n; ++i){
    51. scanf("%d %d", &pid, &nbrOfTopics);
    52. pidVec.push_back(pid);
    53. personMap[pid].nbrOfTopics = nbrOfTopics;
    54. personMap[pid].idRank = i;
    55. for (int j = 0; j < nbrOfTopics; ++j){
    56. scanf("%d", &priority);
    57. personMap[pid].topicPriority.push_back(priority);
    58. }
    59. }
    60. sort(pidVec.begin(), pidVec.end(), cmp);
    61. curr = 0;
    62. while (1){
    63. if (!ttime.count(curr)){
    64. ++curr;
    65. continue;
    66. }
    67. for (auto it = topicMap.begin(); it != topicMap.end(); ++it){
    68. if (curr >= it->second.elapsedTime && curr <= it->second.lastRequest && (curr - it->second.elapsedTime) % it->second.timeGap == 0){
    69. currRequest[it->first]++;
    70. }
    71. }
    72. if (currRequest.empty()){
    73. ++curr;
    74. continue;
    75. }
    76. for (int i = 0; i < pidVec.size(); ++i){
    77. if (personMap[pidVec[i]].finishTime <= curr){
    78. for (int j = 0; j < personMap[pidVec[i]].topicPriority.size(); ++j){
    79. int temp = personMap[pidVec[i]].topicPriority[j];
    80. if (currRequest.count(temp)){
    81. personMap[pidVec[i]].recentJob = curr;
    82. int ft = curr + topicMap[temp].serviceTime;
    83. personMap[pidVec[i]].finishTime = ft;
    84. ttime.insert(ft);
    85. finishTime = std::max(finishTime, ft);
    86. if (currRequest[temp] > 1){
    87. currRequest[temp]--;
    88. } else {
    89. currRequest.erase(temp);
    90. }
    91. break;
    92. }
    93. }
    94. if (currRequest.empty()){
    95. break;
    96. }
    97. }
    98. }
    99. if (currRequest.empty() && curr >= lastRequest){
    100. break;
    101. }
    102. ++curr;
    103. }
    104. printf("Scenario %d: All requests are serviced within %d minutes.\n", ++kase, finishTime);
    105. }
    106. #ifdef debug
    107. fclose(stdin);
    108. fclose(stdout);
    109. #endif
    110. return 0;
    111. }

    一开始测试数据能过但是超时的代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define debug
    7. struct topic{
    8. int nbrOfRequest, elapsedTime, serviceTime, timeGap, lastRequest;
    9. };
    10. struct person{
    11. int nbrOfTopics, idRank, recentJob;
    12. int finishTime = 0;
    13. std::vector<int> topicPriority;
    14. };
    15. int n, tid, nbrOfRequest, elapsedTime, serviceTime, timeGap, pid, nbrOfTopics, priority, lastRequest, finishTime, curr, kase = 0;
    16. std::unordered_map<int, topic> topicMap;
    17. std::unordered_map<int, person> personMap;
    18. std::unordered_map<int, int> currRequest;
    19. std::unordered_map<int, std::vector<int>> line;
    20. std::set<int> ttime;
    21. bool cmp(const int &a, const int &b){
    22. return personMap[a].recentJob != personMap[b].recentJob ? personMap[a].recentJob < personMap[b].recentJob : personMap[a].idRank < personMap[b].idRank;
    23. }
    24. int main(){
    25. #ifdef debug
    26. freopen("0.txt", "r", stdin);
    27. freopen("1.txt", "w", stdout);
    28. #endif
    29. while (scanf("%d", &n) == 1 && n){
    30. lastRequest = 0;
    31. finishTime = 0;
    32. ttime.clear();
    33. ttime.insert(0);
    34. for (int i = 0; i < n; ++i){
    35. scanf("%d %d %d %d %d", &tid, &nbrOfRequest, &elapsedTime, &serviceTime, &timeGap);
    36. topicMap[tid].nbrOfRequest = nbrOfRequest;
    37. topicMap[tid].elapsedTime = elapsedTime;
    38. topicMap[tid].serviceTime = serviceTime;
    39. topicMap[tid].timeGap = timeGap;
    40. topicMap[tid].lastRequest = elapsedTime + (nbrOfRequest - 1) * timeGap;
    41. lastRequest = std::max(lastRequest, topicMap[tid].lastRequest);
    42. for (int j = 0; j < nbrOfRequest; ++j){
    43. ttime.insert(elapsedTime + j * timeGap);
    44. }
    45. }
    46. scanf("%d", &n);
    47. for (int i = 0; i < n; ++i){
    48. scanf("%d %d", &pid, &nbrOfTopics);
    49. personMap[pid].nbrOfTopics = nbrOfTopics;
    50. personMap[pid].idRank = i;
    51. for (int j = 0; j < nbrOfTopics; ++j){
    52. scanf("%d", &priority);
    53. personMap[pid].topicPriority.push_back(priority);
    54. }
    55. }
    56. curr = 0;
    57. while (1){
    58. if (!ttime.count(curr)){
    59. ++curr;
    60. continue;
    61. }
    62. for (auto it = topicMap.begin(); it != topicMap.end(); ++it){
    63. if (curr >= it->second.elapsedTime && curr <= it->second.lastRequest && (curr - it->second.elapsedTime) % it->second.timeGap == 0){
    64. currRequest[it->first]++;
    65. }
    66. }
    67. while (!currRequest.empty()){
    68. line.clear();
    69. std::vector<int> empty;
    70. for (auto it = currRequest.begin(); it != currRequest.end(); ++it){
    71. line[it->first] = empty;
    72. }
    73. for (auto it = personMap.begin(); it != personMap.end(); ++it){
    74. if (it->second.finishTime <= curr){
    75. for (int i = 0; i < it->second.topicPriority.size(); ++i){
    76. if (currRequest.count(it->second.topicPriority[i])){
    77. line[it->second.topicPriority[i]].push_back(it->first);
    78. break;
    79. }
    80. }
    81. }
    82. }
    83. bool flag = true;
    84. for (auto it = line.begin(); it != line.end(); ++it){
    85. if (!it->second.empty()){
    86. flag = false;
    87. sort(it->second.begin(), it->second.end(), cmp);
    88. int temp = it->second[0];
    89. personMap[temp].recentJob = curr;
    90. personMap[temp].finishTime = curr + topicMap[it->first].serviceTime;
    91. ttime.insert(personMap[temp].finishTime);
    92. finishTime = std::max(finishTime, personMap[temp].finishTime);
    93. if (currRequest[it->first] > 1){
    94. currRequest[it->first]--;
    95. } else {
    96. currRequest.erase(it->first);
    97. }
    98. }
    99. }
    100. if (flag){
    101. break;
    102. }
    103. }
    104. if (currRequest.empty() && curr >= lastRequest){
    105. break;
    106. }
    107. ++curr;
    108. }
    109. printf("Scenario %d: All requests are serviced within %d minutes.\n", ++kase, finishTime);
    110. }
    111. #ifdef debug
    112. fclose(stdin);
    113. fclose(stdout);
    114. #endif
    115. return 0;
    116. }

  • 相关阅读:
    HTML期末学生大作业-拯救宠物网页作业html+css
    分布式文件系统HDFS(林子雨慕课课程)
    【SQLite】环境安装
    docker-compose安装nacos
    C++关键词探索:理解变量、函数参数、函数返回值以及类成员函数的修饰符
    Java绘图-第19章
    何为面向对象的编程思想
    Java中[Ljava.lang.Object;是什么?
    C# 自定义控件
    图解:Go Mutext
  • 原文地址:https://blog.csdn.net/linh2006/article/details/134282240