A1074. Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct NODE{
int lt, rt;
int valid;
int data, rk;
}node;
node nds[];
bool cmp(node a, node b){
if(a.valid != b.valid)
return a.valid > b.valid;
else return a.rk < b.rk;
}
bool cmp2(node a, node b){
return a.rk > b.rk;
}
int main(){
int N, K, head;
scanf("%d%d%d", &head, &N, &K);
int temp;
for(int i = ; i < N; i++){
scanf("%d", &temp);
nds[temp].lt = temp;
scanf("%d%d", &nds[temp].data, &nds[temp].rt);
}
int pt = head, cnt = ;
while(pt != -){
nds[pt].valid = ;
nds[pt].rk = cnt;
cnt++;
pt = nds[pt].rt;
}
sort(nds, nds + , cmp);
for(int i = ; i + K <= cnt; i += K){
sort(nds + i, nds + i + K, cmp2);
}
if(cnt == )
printf("-1");
for(int i = ; i < cnt; i++){
if(i < cnt - )
printf("%05d %d %05d\n", nds[i].lt, nds[i].data, nds[i + ].lt);
else printf("%05d %d -1\n", nds[i].lt, nds[i].data);
}
cin >> N;
return ;
}
总结:
1、注意链表节点的初始顺序不是读入的顺序,而是在读完之后,从给定的首地址遍历一遍的顺序。
2、静态链表题一般都会在输入中放入无效节点,需要过滤掉。注意全空的情况。
3、本题可以在第一次遍历合法节点的时候,给每个节点都编号0、1、2......,然后按照valid和节点编号排序,这样可以将所有节点聚集起来且按照链表本身的顺序依次存放。之后第二遍遍历,每K个从大到小再排一次序即可得到最终的顺序。
4、每个节点自己的地址是始终不变的。
5、只有到了最后一步才可以把所有合法节点聚集到数组前部。
6、测试点
00000 6 3
00000 1 11111
11111 2 22222
22222 3 -1
33333 4 44444
44444 5 55555
55555 6 -1
A1074. Reversing Linked List的更多相关文章
- PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- PAT甲级——A1074 Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- PAT_A1074#Reversing Linked List
Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
随机推荐
- 【教你玩转云计算】在阿里云一键安装快速部署Oracle11g
云计算时代提供了更方便可靠的IAAS,PAAS和SAAS平台.将已有或正在研发的项目迁移到云计算平台,和传统的服务器部署还是存在一些异同点. 本文手把手教你在阿里云平台快速的部署Oracle11g ...
- Linux内核及分析 第三周 Linux内核的启动过程
实验过程: 打开shell终端,执行以下命令: cd LinuxKernel/ qemu -kernel linux-3.18.6/arch/x86/boot/bzImage-initrd rootf ...
- 《Linux内核设计与实现》 第五章学习笔记
第五章 系统调用 在现代操作系统中,内核提供了进程与内核进行交互的一组接口.有如下作用: 让应用程序受限的访问硬件设备 提供了创新进程并与已有进程进行通信的机制 提供了申请操作系统其它资源的能力 保证 ...
- 20135316Linux内核学习笔记第八周
20135316王剑桥<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC 1000029000 一.进程调度与进程调度的时机分析 ...
- bate版说明书
本游戏是一款手机游戏,学生可以在无聊时打发时间,放松心情.现在只有十关,游戏运行还算可以. 特点: 对alpha版进行了修改,可以进行暂停,重开,返回目录. 画面也进行了优化,不象之前的那么粗超. 游 ...
- Practice5.1 测试与封装5.1
结对同伴: 姓名 学号 博客地址 叶子鹏 201306114420 http://www.cnblogs.com/kazehanaai/ 王佳宁 201306114434 http://www.cnb ...
- github更新,发布地址,燃尽图,总结
github地址:https://github.com/Lingchaoyang 网盘发布地址:http://pan.baidu.com/s/1qXgHiyC 燃尽图: 团队得分(100分制): 杨灵 ...
- maven 总结
- ubuntu16.04下载安装navicate
1.下载试用版本地址: https://www.navicat.com.cn/download/navicat-premium 2.解压缩 tar -zxvf /home/rain/download ...
- Windows系统下PHP使用Redis
参考链接:https://www.cnblogs.com/lhat/p/6402472.html 环境:windows 10 64位操作系统 php 5.4 redis 3.0 1.redis ...