PAT L2-022 重排链表 (deque双向队列)
来源:互联网 发布:淘宝商家怎么交保证金 编辑:程序博客网 时间:2024/05/24 05:45
L2-022. 重排链表
时间限制
500 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→...。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (<= 105)。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址;Data是该结点保存的数据,为不超过105的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。
输出格式:
对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 600000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218输出样例:
68237 6 0010000100 1 9999999999 5 1230912309 2 0000000000 4 3321833218 3 -1
提交代码
关于deque的用法, 参考: http://blog.csdn.net/morewindows/article/details/6946811
分析: 链表建立完成后, 再遍历将元素都放在 deque 的 back , 之后依次访问 front 和 back 实现交替输出, AC代码如下:
#include <bits/stdc++.h>#define PI acos(-1)#define LL long long#define INF 0x3f3f3f3f#define SCD(a) scanf("%d",&a)#define SCDD(a,b) scanf("%d%d",&a,&b)#define SCF(a) scanf("%lf",&a)#define PTD(a) printf("%d", a);#define PTDE(a) printf("%d\n",a)#define MST(a,b) memset(a, b, sizeof(a))using namespace std;// PAT L2 022const int M = 100010;int n, m;struct List{ int data; int next;};List l[M];deque<int> qt;int main(){ int fir; SCDD(fir, n); int t, id, i, k, ne; for(i=0;i<n;i++){ SCD(id); SCDD(t, ne); l[id].data = t; l[id].next = ne; } int lo = 0; while(true){ qt.push_back(fir); lo ++ ; fir = l[fir].next; if(fir == -1) break; } int pre = 0; bool flag = true; for(i=0; i<lo; i++){ if(i % 2 == 1){ t = qt.front(); printf("%05d\n%05d %d ", t, t, l[t].data); qt.pop_front(); }else{ t = qt.back(); if(flag){ printf("%05d %d ", t, l[t].data); flag = false; }else{ printf("%05d\n%05d %d ", t, t, l[t].data); } qt.pop_back(); } } PTDE(-1); return 0;}/**/
阅读全文
1 0
- PAT L2-022 重排链表 (deque双向队列)
- Deque(双向队列)
- L2-022. 重排链表(变型)
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- C++ Deque(双向队列)
- collections deque(双向队列)
- 团体程序设计天梯赛L2-022 重排链表
- STL之deque(双向队列)
- python3 deque(双向队列)用法
- deque双向队列
- C++ Deque(双向队列)
- deque双向队列
- C++ Deque 双向队列
- deque双向队列
- 嵌入式学习笔记(第八天)系统网络编程
- Python 里面的一些小技巧(持续更新)
- java web基础大总结
- 最长上升子数列
- 基于XILINX FPGA的卷积神经网络(三)
- PAT L2-022 重排链表 (deque双向队列)
- 【HDU 1950】 Bridging signals (最长上升子序列nlogn算法)(二分,动态规划)
- 【CUGBACM15级BC第16场 A】Revenge of Segment Tree
- awk详解(1)
- 最简单的python 爬虫
- myeclipse 导入 play项目
- 递归问题学习三之生成指定字符串的全排序
- 【NOIP2017】Day7
- Centos 7安装 pgplot