题目传送门

sol1:拿到这题的时候刚上完课,讲的是指针。所以一下子就联想到了双向链表。链表可以解决卡片移动的问题,但是无法快速定位要移动的卡片,所以再开一个指针数组,结合数组下标访问的特性快速定位到要移动的卡片。把链表和数组的优势结合起来;

  • 双向链表

    #include "bits/stdc++.h"
    using namespace std;
    const int MAXN = 1e5 + ;
    struct Node {
    Node* pre;
    int num;
    Node* nxt;
    };
    Node* a[MAXN];
    int main() {
    int n, m, k;
    scanf("%d%d", &n, &m);
    a[] = new(Node);
    for (int i = ; i <= n; i++) {
    a[i] = new(Node);
    a[i]->pre = a[i - ];
    a[i - ]->nxt = a[i];
    scanf("%d", &a[i]->num);
    }
    a[n + ] = new(Node);
    a[n + ]->pre = a[n];
    a[n]->nxt = a[n + ];
    for (int i = ; i <= m; i++) {
    scanf("%d", &k);
    a[k]->nxt->pre = a[k]->pre;
    a[k]->pre->nxt = a[k]->nxt;
    a[]->nxt->pre = a[k];
    a[k]->nxt = a[]->nxt;
    a[]->nxt = a[k];
    a[k]->pre = a[];
    }
    for (Node* i = a[]->nxt; i != a[n + ]; i = i->nxt)
    printf("%d ", i->num);
    return ;
    }

sol2:比赛结束后看了别人的题解,发现还可以用栈来实现。越晚操作的牌就在牌堆的越上面。所以只要把牌堆的操作倒着输出就好了,如果这个数已经输出过就不用输了。

  • #include "bits/stdc++.h"
    using namespace std;
    const int MAXN = 2e5 + ;
    int stk[MAXN];
    bool vis[MAXN];
    int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = n; i >= ; i--)
    scanf("%d", &stk[i]);
    for (int i = n + ; i <= n + m; i++)
    scanf("%d", &stk[i]);
    for (int i = n + m; i >= ; i--) {
    if (vis[stk[i]]) continue;
    printf("%d ", stk[i]);
    vis[stk[i]] = true;
    }
    return ;
    }

最新文章

  1. Objective-C语法之KVO使用
  2. C#连接Access数据库(详解)
  3. 【POJ3254】Corn Fields 状压DP第一次
  4. asp.net中调用命令行
  5. Effective Java 49 Prefer primitive types to boxed primitives
  6. Thinkphp3.2.3如何加载自定义函数库
  7. iOS webView的一些基本用法
  8. 温习H3C S5500的VLAN配置
  9. codeforces Ilya and Matrix
  10. jqGrid选中行、格式化、自定义按钮、隐藏
  11. 第12课 std::bind和std::function(3)_std::function可调用对象包装器
  12. 【React Native开发】React Native应用设备执行(Running)以及调试(Debugging)(3)
  13. GoogLeNet解读
  14. B/S与C/S的比较
  15. golang几种post方式
  16. javaWeb 使用线程池+队列解决&quot;订单并发&quot;问题
  17. WebStorm ES6 语法支持设置
  18. siebel简介
  19. PHP生成随机字符串与唯一字符串
  20. AMQP &amp;&amp; MQTT comparision

热门文章

  1. 27. docker compose 单机 均衡负载
  2. h5-背景样式
  3. h5-立方体
  4. SDWebImage缓存图片和读取图片
  5. SpringSecurity过滤器顺序
  6. PAT Basic 1013 数素数 (20) [数学问题-素数]
  7. Power BI 企业邮箱账户注册
  8. Python模块——json
  9. \_\_slots\_\_
  10. Python笔记_第三篇_面向对象_9.Python中的&quot;get&quot;和&quot;set&quot;方法(@property和@.setter)