团体程序设计天梯赛L2-022 重排链表
2017-04-27 22:13
411 查看
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 6 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218
输出样例:
68237 6 00100 00100 1 99999 99999 5 12309 12309 2 00000 00000 4 33218 33218 3 -1
解题思路:模拟
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
struct node
{
int a, nt;
}x[100090];
int s, n;
int ans1[100090], ans2[100090];
int main()
{
while (~scanf("%d%d", &s, &n))
{
int a, xx, nt;
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &xx, &a, &nt);
x[xx].a = a, x[xx].nt = nt;
}
int cnt = 0;
while (s != -1)
{
ans1[cnt++] = s;
s = x[s].nt;
}
int cnt1 = 0,l=0,r=cnt-1;
while (l <= r)
{
if(l==r) ans2[cnt1++] = ans1[r--];
else
{
ans2[cnt1++] = ans1[r--];
ans2[cnt1++] = ans1[l++];
}
}
for (int i = 0; i < cnt-1; i++) printf("%05d %d %05d\n", ans2[i], x[ans2[i]].a, ans2[i+1]);
printf("%05d %d -1\n", ans2[cnt - 1], x[ans2[cnt-1]].a);
}
return 0;
}
相关文章推荐
- 团体程序设计天梯赛-练习集 -- L2-022. 重排链表
- 2017年团体程序设计天梯赛-总决赛 L2-2. 重排链表
- 团体程序设计天梯赛-练习集 L2-002. 链表去重 解题报告
- 天梯赛 L2-002 链表去重 和 L2-022 重排链表
- 团体程序设计天梯赛-练习集L2-002. 链表去重
- 团体程序设计天梯赛L2-002 链表去重
- 团体程序设计天梯赛L2-002 链表去重(模拟)
- 天梯赛L2-022【重排链表】
- PAT 天梯赛 L2-022. 重排链表 【数据结构】
- 团体程序设计天梯赛-练习集 L2-2. 链表去重 数组模拟链表
- 团体程序设计天梯赛-练习集 L2-002 链表去重 解题报告
- 团体程序设计天梯赛L2-002 链表去重
- 团体程序设计天梯赛-练习集L2-002. 链表去重
- 团体程序设计天梯赛-练习集 L2-002. 链表去重
- 团体程序设计天梯赛-练习集 L2-002. 链表去重
- L2-002. 链表去重-PAT团体程序设计天梯赛GPLT
- [数据结构]L2-022. 重排链表
- 团体程序设计天梯赛-练习集 L2-3. 月饼 贪心
- 团体程序设计天梯赛-练习集-L2-013. 红色警报(dfs)
- 团体程序设计天梯赛-练习集 L2-013. 红色警报 解题报告