• POJ 3481、HDU 1908、AcWing 5125:双端队列 ← STL map


    【题目来源】
    本题来源于三个刷题网站:
    POJ 3481:http://poj.org/problem?id=3481
    HDU 1908:http://acm.hdu.edu.cn/showproblem.php?pid=1908
    AcWing 5125:https://www.acwing.com/problem/content/5128/


    【题目描述】
    某银行的业务处理系统原理如下。
    初始时,待处理业务队列(简称为队列)为空。
    接下来,系统会收到一系列的请求,请求分为以下四种:
    ● 0,表示系统需要停止服务。
    ● 1 K P,表示收到一个来自客户 K 的优先级为 P 的待处理业务,并将该业务加入队列。
    ● 2,表示处理当前队列中优先级最高的待处理业务,并将该业务从队列中删除。
    ● 3,表示处理当前队列中优先级最低的待处理业务,并将该业务从队列中删除。
    保证在任何时候,当前队列中的现有待处理业务都满足:不会有多个待处理业务来自同一客户,也不会有多个待处理业务优先级相同。
    也就是说,在完成某客户的待处理业务之前,不会再次收到该客户的待处理业务;在完成某优先级的待处理业务之前,不会再次收到该优先级的待处理业务。
    给定银行系统收到的所有请求,请你模拟系统处理过程。

    【输入格式】
    输入若干行,每行包含一个请求,格式如题面描述。
    数据保证有且仅有最后一行包含请求 0。

    【输出格式】
    对于每个请求 2 和 3,输出一行结果,一个整数,表示此次请求处理业务的客户编号。如果没有可处理业务,则输出 0。

    【数据范围】
    输入最多包含 10^6 个请求。
    1≤K≤10^6,
    1≤P≤10^7。

    【输入样例】
    2
    1 20 14
    1 30 3
    2
    1 10 99
    3
    2
    2
    0

    【输出样例】

    0
    20
    30
    10
    0

    【算法分析】
    本文使用到
    STL map,详见:https://cplusplus.com/reference/map/map/

    【算法代码1】

    1. #include
    2. #include
    3. using namespace std;
    4. map<int,int> mp;
    5. map<int,int>::iterator it;
    6. int K,P;
    7. int cnt;
    8. int main() {
    9. int op;
    10. while(~scanf("%d",&op)) {
    11. if(op==0) break;
    12. if(op==1) {
    13. scanf("%d%d",&K,&P);
    14. mp[P]=K;
    15. cnt++;
    16. }
    17. if(op==2) {
    18. if(cnt==0) printf("0\n");
    19. else {
    20. it=mp.end();
    21. it--;
    22. printf("%d\n",it->second);
    23. mp.erase(it);
    24. cnt--;
    25. }
    26. }
    27. if(op==3) {
    28. if(cnt==0) printf("0\n");
    29. else {
    30. it=mp.begin();
    31. printf("%d\n",it->second);
    32. mp.erase(it);
    33. cnt--;
    34. }
    35. }
    36. }
    37. return 0;
    38. }
    39. /*
    40. in:
    41. 2
    42. 1 20 14
    43. 1 30 3
    44. 2
    45. 1 10 99
    46. 3
    47. 2
    48. 2
    49. 0
    50. out:
    51. 0
    52. 20
    53. 30
    54. 10
    55. 0
    56. */


    【算法代码2】

    1. #include
    2. #include
    3. using namespace std;
    4. map<int,int> mp;
    5. int main() {
    6. int K,P;
    7. int op;
    8. while(~scanf("%d",&op)) {
    9. if(op==0) break;
    10. if(op==2 && mp.size()==0) {
    11. printf("0\n");
    12. continue;
    13. }
    14. if(op==3 && mp.size()==0) {
    15. printf("0\n");
    16. continue;
    17. }
    18. if(op==1) {
    19. scanf("%d%d",&K,&P);
    20. mp[P]=K;
    21. continue;
    22. }
    23. if(op==2) {
    24. printf("%d\n",(*mp.rbegin()).second);
    25. mp.erase(mp.find((*mp.rbegin()).first));
    26. } else {
    27. printf("%d\n",(*mp.begin()).second);
    28. mp.erase(mp.begin());
    29. }
    30. }
    31. return 0;
    32. }
    33. /*
    34. in:
    35. 2
    36. 1 20 14
    37. 1 30 3
    38. 2
    39. 1 10 99
    40. 3
    41. 2
    42. 2
    43. 0
    44. out:
    45. 0
    46. 20
    47. 30
    48. 10
    49. 0
    50. */




    【参考文献】
    https://blog.csdn.net/coding_sun/article/details/77248892
    http://phpzyw.com/bcddeBGwCBVVRBQM.html






     

  • 相关阅读:
    rancher导入集群时证书报错
    go-GMP和Scheduler
    链路聚合 VRRP
    OpenKruise原理介绍和安装
    设计模式——桥接模式
    docker容器数据卷
    vue3响应式原理:Proxy + Reflect
    Metabase学习教程:系统管理-7
    【C语言】【数据结构】【顺序表】
    C++ Qt开发:TreeWidget 树形选择组件
  • 原文地址:https://blog.csdn.net/hnjzsyjyj/article/details/133697525