hdu_1908_map知识补全_银行排队问题

本文介绍了一个银行排队系统的实现方法,该系统使用C++语言及map数据结构,能够根据客户的不同请求,灵活地调整服务策略,如按优先级高低选择服务客户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input
Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output
For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0
Source
Southeastern Europe 2007
mean
hdu_1908_map知识补全银行排队问题给你这个的标号和优先级_2时输出优先级大的3时输出优先级小的_1有新的人进入
先输入操作符号 0,结束输入 1,有人加入排队,输入他的序号k和优先级k,2输出优先级大的,3输出优先级小的序号
ans
map是自动按键值排序的,感觉deque也是可以怼的,就是要写重载,麻烦点

#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,i,j;
    map<int,int> q;
    while(cin>>n&&n)
    {
        if(n==1)
        {
            int k,x;
            scanf("%d%d",&k,&x);
            q[x]=k;
        }
        else if(n==2)
        {
            if(!q.empty())
            {
                 cout<<q.rbegin()->second<<endl;
                 q.erase(q.find(q.rbegin()->first));
            }
            else puts("0");
        }
        else
        {
            if(!q.empty())
            {
                cout<<q.begin()->second<<endl;
                q.erase(q.begin());
            }
            else puts("0");
        }
    }
    return 0;
}
AllSight
C语言 · C++中map的用法详解
转载自:http://blog.youkuaiyun.com/sunquana/article/details/12576729

一、定义  

    (1) map<string,   int>   Map; 
    (2) 或者是:typedef   map<string,int>   Mymap; 
                      Mymap   Map; 

二、插入数据 

插入数据之前先说一下pair 和 make_pair 的用法
pair是一个结构体,有first和second 两个域,可以直接访问

1 string key="sunquan";  
2 int value=123456;  
3 pair <string,int>  b(key, value);//这里 pair <string,string>是数据类型,后面是调带参构造方法  
4 cout<<b.first<<endl; 
而make_pair是返回一个pair <类型,类型>  的数据,eg:make_pair("asa",123456);   不过还得找个pair <string,int>类型的变量来接受返回值。
下面步入正题:



 (1) Map["abc"]=1;
 (2) Map.insert(pair<string,int>("c",3));
 (3)Map.insert(make_pair<string,int>("d",4)); 

三、修改和查找数据

  (1)修改Map["sunquan"]=11111;

  (2)查找数据 用Map.find(key); 可以通过键来查。

切记不要用int value=Map[key];这样会在Map中增加这个key,而value就是缺省值(int0string为空字符串)。

通过方法(2),会返回迭代器的地址,key不存在的话迭代器的值为Map.end();

四、删除元素

(1)通过key删除;

(2)通过迭代器来删除;

下面看一下详细的代码:

复制代码
 1 #include <iostream>  
 2 #include <cstdio>  
 3 #include <cstring>  
 4 #include <string>  
 5 #include <map>  
 6 using namespace std;  
 7   
 8 int main()  
 9 {  
10     map<string,int> Map;  
11     map<string,int> ::iterator it;  
12     Map.insert(pair<string,int>("root",12));  
13     Map.insert(pair<string,int>("scot",11));  
14     for(it=Map.begin();it!=Map.end();it++)  
15         cout<<it->first<<"    "<<it->second<<endl;  
16     it=Map.begin();  
17     Map.erase(it);//通过迭代器删除  
18     string key="root";  
19     Map.erase(key);//通过key删除  
20       
21     Map.erase(Map.begin(),Map.end());//一个迭代器,到另一个迭代器  
22     //相当于  Map.clear();  
23   
24     for(it=Map.begin();it!=Map.end();it++)  
25         cout<<it->first<<"    "<<it->second<<endl;  
26     return 0;  
27 }  
复制代码
注:

map<int, string>::iterator it 是声明一个 迭代器
map<int, string> it 是 声明一个map容器



五、c++中map的一些方法

    begin() 返回指向map头部的迭代器
    clear() 删除所有元素
    count() 返回指定元素出现的次数
    empty() 如果map为空则返回true
    end()   返回指向map末尾的迭代器

    equal_range()    返回特殊条目的迭代器对

    erase() 删除一个元素
    find()  查找一个元素
    insert()插入元素
    max_size()返回可以容纳的最大元素个数
    size()  返回map中元素的个数
    swap()  交换两个map

      get_allocator()  返回map的配置器
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值