【2018百度之星初赛(A)】1002 度度熊学队列

本文详细解析了HDU 6375题目,通过使用STL容器map和deque来解决该问题。map用于防止内存溢出,deque则用于高效的数据插入与删除操作。

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

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6375

 

Knowledge Point:

  STL - map:https://www.cnblogs.com/liubilan/p/9458765.html

  STL - deque:https://www.cnblogs.com/liubilan/p/9461141.html

 

这道题主要考的是STL容器的使用,没有写出来只说明了一个道理:

   STL很重要啊!目前你用到的没用到的你都得了解并且会使用啊!!

这题主要使用了map, deque两种容器,map是为了防止超内存,因为map的特性是需要就自动建立新的节点,否则不会开辟多余的空间;

 

附代码:

 1 #include<iostream>
 2 #include<map>
 3 #include<deque>
 4 using namespace std;
 5 
 6 int n, q;
 7 map<int, deque<int> > imap;
 8 
 9 void read(int &x){
10     char ch = getchar();x = 0;
11     for (; ch < '0' || ch > '9'; ch = getchar());
12     for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
13 }
14 
15 int main()
16 {
17     int option, u, v, w, val;
18     while(cin>>n>>q)
19     {
20         imap.clear();
21         while(q--) {
22             read(option); read(u); read(w);
23             
24             if(option == 1) {
25                 read(val);
26                 if(!w) imap[u].push_front(val);
27                 else imap[u].push_back(val);
28             }
29             else if(option == 2) {
30                 if(imap[u].empty()) {
31                     cout<<-1<<endl;
32                     continue;
33                 }
34                 if(!w) {
35                     cout<<imap[u].front()<<endl;
36                     imap[u].pop_front();
37                 }
38                 else {
39                     cout<<imap[u].back()<<endl;
40                     imap[u].pop_back();
41                 }
42             }
43             else if(option == 3) {
44                 read(v);
45                 if(!v)        //v接在u后 
46                     imap[u].insert(imap[u].end(), imap[w].begin(), imap[w].end());
47                 else        //v翻转后接在u后 
48                     imap[u].insert(imap[u].end(), imap[w].rbegin(), imap[w].rend());
49                 imap[w].clear();
50             }
51         }
52     }
53     
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/liubilan/p/9461107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值