码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-11 22:04:53      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:return   empty   ||   continue   节点   amp   需要   etc   hdu   

题目地址: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 }

 

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

标签:return   empty   ||   continue   节点   amp   需要   etc   hdu   

原文地址:https://www.cnblogs.com/liubilan/p/9461107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!