题目传送门

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 ;
    }

HDU-6707-Shuffle Card(很数据结构的一道题)的更多相关文章

  1. CCPC 2019 网络赛 1006 Shuffle Card

    // 签到题,比赛时候写双向链表debug了半天,发现有更好方法,记录一下.   Shuffle Card HDU 6707 题意:   有一 \(n\) 张卡片,编号 \(1~n\) ,给定初始编号 ...

  2. YTU 2457: 很简单的一道题

    2457: 很简单的一道题 时间限制: 1 Sec  内存限制: 128 MB 提交: 261  解决: 80 [提交][状态][讨论版] 题目描述 有一个简单的函数数学公式,如下 输入 重复输入多组 ...

  3. Shuffle Card HDU - 6707

    题目链接:https://vjudge.net/problem/HDU-6707 题意:给你一个数组a[ ](a[1]=1,a[2]=2.....a[n]=n),然后m次操作,每次把那个数拿到最前面去 ...

  4. HDU 4336:Card Collector(容斥原理)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...

  5. HDU - 4336:Card Collector(min-max容斥求期望)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  6. hdu 1428(很好的一道题,最短路+记忆化搜索)

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...

  8. 【HDU】4336 Card Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意:n张卡片,每一次取一个盒子,盒子里装有卡片i的概率是p[i],求得到所有卡片所需要开的盒子的期望数( ...

  9. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数参数

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. android stutio 添加依赖

    添加依赖有 3种方法: 1 :copy jar 包到libs目录 ,add to library 2: copy  aar 文件到libs  ,gradle 文件 android 节点添加  repo ...

  3. 2019年春PAT甲级考试

    这次考试不是很理想,一道题目没能做完. 自己原因差不多三条: 1.自己实力不够,准备时间也有点仓促,自己没能做到每道题目都有清晰的思路. 2.考试的心理素质不行,因为设备原因东奔西跑浪费了挺多时间,自 ...

  4. HGP|VCG|UK10K|中科院职业人群队列研究计划|药物基因组学

    全球性计划:表观组计划:肝计划 测1000人的变异level HGP计划三个阶段,范围逐步扩大和深化. Pilot:deep sequence---low coverage Phase 1 Phase ...

  5. awk下 gsub函数用法

     (2012-03-27 01:37:28) 标签: awk gsub linux 函数 it 分类: linux gsub函数则使得在所有正则表达式被匹配的时候都发生替换 gsub(regular ...

  6. thinkcmf面包屑制作

    网站常有的功能面包屑,如图所示: 支持1级分类 list.html 首页 / {$name} article.html 首页 / {$term['name']} / {$post_title} 链接: ...

  7. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  8. java select单线程 服务器

    package com.Select; /** *select单线程 服务器 **/ import java.io.IOException; import java.net.InetSocketAdd ...

  9. [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)

    https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...

  10. 40)PHP,mysql_fetch_row和mysql_fetch_array和mysql_fetch_assoc的区别

    分析: mysql_fetch_row,这个函数是从结果集中取一行作为枚举数据,从和指定的结果标识关联的结果集中取得一行数据并作为数组返回.每个结果的列储存在一个数组的单元中,偏移量从 开始. 注意, ...