HDU-6707-Shuffle Card(很数据结构的一道题)
题目传送门
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(很数据结构的一道题)的更多相关文章
- CCPC 2019 网络赛 1006 Shuffle Card
// 签到题,比赛时候写双向链表debug了半天,发现有更好方法,记录一下. Shuffle Card HDU 6707 题意: 有一 \(n\) 张卡片,编号 \(1~n\) ,给定初始编号 ...
- YTU 2457: 很简单的一道题
2457: 很简单的一道题 时间限制: 1 Sec 内存限制: 128 MB 提交: 261 解决: 80 [提交][状态][讨论版] 题目描述 有一个简单的函数数学公式,如下 输入 重复输入多组 ...
- Shuffle Card HDU - 6707
题目链接:https://vjudge.net/problem/HDU-6707 题意:给你一个数组a[ ](a[1]=1,a[2]=2.....a[n]=n),然后m次操作,每次把那个数拿到最前面去 ...
- HDU 4336:Card Collector(容斥原理)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...
- HDU - 4336:Card Collector(min-max容斥求期望)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...
- 【HDU】4336 Card Collector
http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意:n张卡片,每一次取一个盒子,盒子里装有卡片i的概率是p[i],求得到所有卡片所需要开的盒子的期望数( ...
- hdu 2629 Identity Card (字符串解析模拟题)
这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...
随机推荐
- 从定时器的选型,到透过源码看XXL-Job(下)
透过源码看xxl-job (注:本文基于xxl-job最新版v2.0.2, quartz版本为 v2.3.1. 以下提到的调度中心均指xxl-job-admin项目) 上回说到,xxl-job是一个中 ...
- uwsgi Import Error: No module named 'encodings'
https://serverfault.com/questions/558427/uwsgi-import-error-no-module-named-encodings I don't know i ...
- 素小暖讲JVM:Eclipse运行速度调优
本系列是用来记录<深入理解Java虚拟机>这本书的读书笔记.方便自己查看,也方便大家查阅. 欲速则不达,欲达则欲速! 这两天看了JVM的内存优化,决定尝试一下,对Eclipse进行内存调优 ...
- [Algo] 118. Array Deduplication IV
Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...
- CSS公共样式模版
CSS文件命名为global.css,一般此CSS文件是用于装全站主要框架CSS样式代码和初始化的CSS样式. 通常会放初始化CSS代码如下: body, div, ul, ol, dl, dt, d ...
- docker安装(centos-7)
centos7安装docker:Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker .通过 uname -r 命令 ...
- F - kebab HDU - 2883 (最大流构图)
Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stic ...
- Opencv笔记(四)——绘图函数
常用的绘图函数有: cv2.line() cv2.circle() cv2.rectangle() cv2.ellipse() cv2.putText( ...
- mplayer 的安装步骤
编译mplayer: make distclean ./configure --disable-png --disable-gif //加后面的是因为编译时出错了,也可以直接 ./con ...
- POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...