// 签到题,比赛时候写双向链表debug了半天,发现有更好方法,记录一下。

 

Shuffle Card HDU 6707

题意:

  有一 \(n\) 张卡片,编号 \(1~n\) ,给定初始编号排列和 \(m\) 次操作,每次操作将编号为 \(s_i\) 的卡片放到第一张上面。求最后卡片的编号序列。

 

思路:

  直接想法就是模拟,复杂度 \(O(nm)\) ,会T掉。如果采用双向链表储存编号,每次洗牌操作复杂度都是 \(O(1)\) ,总复杂度\(O(m)\),可行。比赛时候写链表少连了一条边调了半天

  双向链表交换两个节点太麻烦了,今天发现可以直接用,输出序列标出上面已拿出的卡片编号即可,复杂度 \(O(n+m)\) 。

 

AC代码1:

 // 栈实现
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 100010; bool vis[maxn];
int arr[maxn];
vector<int> S; int main() {
int n, m;
cin>>n>>m;
for(int i=0;i<n;i++) {
scanf("%d", &arr[i]);
}
for(int i=n-1;i>=0;i--) {
S.push_back(arr[i]);
}
while(m--) {
int card; scanf("%d", &card);
S.push_back(card);
}
int left = n;
while(!S.empty() && left) {
int now = *--S.end();
if(!vis[now]) {
printf("%d ", now);
vis[now] = 1;
--left;
}
S.pop_back();
}
return 0;
}

 

AC代码1:

 // 双向链表实现
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 100010; struct node {
int pre, next;
}cards[maxn];
int head; int arr[maxn]; int main() {
int n, m;
cin>>n>>m;
for(int i=1;i<=n;i++) {
scanf("%d", &arr[i]);
} head = arr[1];
cards[arr[1]].pre = -1;
cards[arr[n]].next = -1;
for(int i=1;i<n;i++) {
cards[arr[i]].next = arr[i+1];
cards[arr[i+1]].pre = arr[i];
} while(m--) {
int c; scanf("%d", &c);
if(c==head) continue; int h = c; // 新的头结点
int pre = cards[c].pre;
int next = cards[c].next; if(pre!=-1)
cards[pre].next = next;
if(next!=-1)
cards[next].pre = pre; cards[c].next = head;
cards[head].pre = c; // 别忘了这一行!!!
cards[c].pre = -1;
head = c; } int p = head;
while(p!=-1) {
printf("%d ", p);
p = cards[p].next;
}
return 0;
}

CCPC 2019 网络赛 1006 Shuffle Card的更多相关文章

  1. CCPC 2019 网络赛 1002 array (权值线段树)

    HDU 6703 array   题意:   给定一个数组 \(a_1,a_2, a_3,...a_n\) ,满足 \(1 \le a[i]\le n\) 且 \(a[i]\) 互不相同.   有两种 ...

  2. CCPC 2019 网络赛 HDU huntian oy (杜教筛)

    1005 huntian oy (HDU 6706) 题意: 令,有T次询问,求 f(n, a, b). 其中 T = 10^4,1 <= n,a,b <= 1e9,保证每次 a,b互质. ...

  3. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  4. 16年大连网络赛 1006 Football Games

    题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=725&pid=1006 Football Games Time ...

  5. 2017ICPC沈阳网络赛 HDU 6205 -- card card card(最大子段和)

    card card card Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?

    题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...

  7. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  8. 【赛后总结+部分题解】2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

    赛后总结: T:今天状态一般,甚至有点疲惫.然后12点比赛开始,和队友开始看题,从最后往前面看,发现数学题公式看不懂.然后发现队友已经双开做1001和1006了,我看着1007有人A,开始做1007. ...

  9. HDU 4764 Stone (2013长春网络赛,水博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. vue 学习 cli3常用配置

    ---恢复内容开始--- cli3以后,构建的项目更加的简洁,配置文件也没有向cli2那样暴漏出来,但这并不代表cli3是不可配置的,我们只需要在根目录下添加一个vue.config.js作为项目的配 ...

  2. 起手一个mpvue项目准备

    1,环境配置(http://mpvue.com/mpvue/quickstart.html) //全局安装vue-cli3脚手架 npm install -g @vue/cli @vue/cli-in ...

  3. leetcode-并查集

    - 题目:130 并查集: class Solution: def solve(self, board: List[List[str]]) -> None: """ ...

  4. ZF、TP、CI等各种框架的区别

    (原标题:面试常见问题之ZF.TP.CI等框架的区别 http://blog.163.com/m13341159039_1/blog/static/245953061201522092212820/) ...

  5. webstorm常见快捷方法与遇到的一些问题

    1.动态添加标签快捷写法 例子:生成10个文字按顺序编号的class为task-item的div 2.win10下webstorm的terminal无法输入? 打开一个 cmd.exe,标题栏 右键 ...

  6. go beego

    一. 引入 go get github.com/astaxie/beego go get gitgub.com/beego/bee go get -u gitxxx.... 更新框架 编写 packa ...

  7. openstack nova 源码解析 — Nova API 执行过程从(novaclient到Action)

    目录 目录 Nova API Nova API 的执行过程 novaclient 将 Commands 转换为标准的HTTP请求 PasteDeploy 将 HTTP 请求路由到具体的 WSGI Ap ...

  8. 常用的一些 linux 指令

    1. mv linux下重命名文件或文件夹使用mv既可实现. 1.1 重命名 a.将一个名为abc.txt的文件重命名为1234.txt #mv abc.txt .txt b. 将目录A重命名为B ( ...

  9. win7 设置双屏壁纸

    http://dualmonitortool.sourceforge.net/ http://www.displayfusion.com/Features/Wallpaper/ 以第一个软件为例: 1 ...

  10. Aizu - ALDS1_13_A-8 Queens Problem-八皇后的路径输出

    The goal of Queens Problem is to put eight queens on a chess-board such that none of them threatens ...