// 签到题,比赛时候写双向链表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 $emit 子传父

    我们使用子组件传递值给父组件使用 $emit 代码 <!DOCTYPE html> <html lang="en"> <head> <me ...

  2. 从psd图中将图层导出成单独文件

  3. leetcode-140-单词拆分②*

    题目描述: 第一次提交:超时 O(N**N) class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> List[s ...

  4. NX二次开发-UFUN单对象选择对话框UF_UI_select_with_single_dialog

    #include <uf.h> #include <uf_ui.h> ], void* user_data, UF_UI_selection_p_t select) { if ...

  5. NX二次开发-C++time函数计时

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> #include <t ...

  6. (转)C++实现RTMP协议发送H.264编码及AAC编码的音视频,摄像头直播

    转:http://www.cnblogs.com/haibindev/archive/2011/12/29/2305712.html C++实现RTMP协议发送H.264编码及AAC编码的音视频 RT ...

  7. 记录一次失败的向git提交代码,和解决的方法。(首次创建仓库)

    背景: 向git push代码(创建一个新的仓库) 做法: 在github创建一个新的仓库------>本地新建文件夹------->依次执行了下面的命令 git init git clo ...

  8. BOM DOM 简介

    BOM和DOM简介 BOM,Browser Object Model ,浏览器对象模型. BOM主要提供了访问和操作浏览器各组件的方式. 浏览器组件:   window(浏览器窗口)   locati ...

  9. CSS如何让文字垂直居中?

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  10. postgres优化项及linux上pg操作记录

    1.linux切换到pg命令: $ su - postgres $ psql postgres=# 2.查看/退出pg ps -ef |grep postgres postgres=# \q 3.一般 ...