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 ...
随机推荐
- 针对Nginx日志的相关运维操作记录
在分析服务器运行情况和业务数据时,nginx日志是非常可靠的数据来源,而掌握常用的nginx日志分析命令的应用技巧则有着事半功倍的作用,可以快速进行定位和统计. 1)Nginx日志的标准格式(可参考: ...
- Centos下SFTP双机高可用环境部署记录
SFTP(SSH File Transfer Protocol),安全文件传送协议.有时也被称作 Secure File Transfer Protocol 或 SFTP.它和SCP的区别是它允许用户 ...
- squid代理http和https方式上网的操作记录
需求说明:公司IDC机房有一台服务器A,只有内网环境:192.168.1.150现在需要让这台服务器能对外访问,能正常访问http和https请求(即80端口和443端口)操作思路:在IDC机房里另找 ...
- 常用rsync命令操作梳理
作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync.rsync不但可以在本机进行文件同步 ...
- linux-shell-引用-命令替换-命令退出状态-逻辑操作符
命令替换:bash7步扩展的之一 嵌套 这里没什么意义 退出状态可以参与逻辑判断 表达式 算数表达式和条件表达式,逻辑表达式 查看passwd命令比,避免用户捕获输入密码的接口 这种方式就可以直接输 ...
- linux-RPM安装
vh可写可不写
- 必应词典案例分析——个人博客作业week3
案例分析 ——必应词典客户端 软件缺陷常常又被叫做Bug,即为计算机软件或程序中存在的某种破坏正常运行能力的问题.错误,或者隐藏的功能缺陷. 缺陷的存在会导致软件产品在某种程度上不能满足用户的需要.I ...
- 使用Eclipse可以方便的统计工程或文件的代码行数,
使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...
- 使用github的感想
github的仓库链接:https://github.com/liyan941016/test github是一个基于git的代码托管平台,要想使用github第一步就要注册账户,然后要创建一个仓库 ...
- Python学习笔记(二)——数据类型
1.数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 2.Python数字类型 Pyth ...