zoukankan      html  css  js  c++  java
  • 【2018百度之星初赛(A)】1002 度度熊学队列

    题目地址: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 }
  • 相关阅读:
    javascript对象继承的实现
    浏览器兼容问题汇总<转>
    DOM笔记
    Ajax日记
    学习态度
    项目1
    导航项目-整体布局
    有关布局
    导航项目开始
    windows 服务 定时程序 跑不出数据
  • 原文地址:https://www.cnblogs.com/liubilan/p/9461107.html
Copyright © 2011-2022 走看看