A1052. Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef struct NODE{
int lt, rt;
int data;
int valid;
NODE(){
valid = ;
}
}node;
node nds[];
bool cmp(node a, node b){
if(a.valid != b.valid){
return a.valid > b.valid;
}else return a.data < b.data;
}
using namespace std;
int main(){
int N, head, addr;
scanf("%d%d", &N, &head);
for(int i = ; i < N; i++){
scanf("%d", &addr);
nds[addr].lt = addr;
scanf("%d %d", &nds[addr].data, &nds[addr].rt);
}
int pt = head, cnt = ;
while(pt != -){
nds[pt].valid = ;
cnt++;
pt = nds[pt].rt;
}
sort(nds, nds + , cmp);
if(cnt > )
printf("%d %05d\n", cnt, nds[].lt);
else printf("0 -1");
for(int i = ; i < cnt; i++){
if(i == cnt - ){
printf("%05d %d -1\n", nds[i].lt, nds[i].data);
}else{
printf("%05d %d %05d\n", nds[i].lt, nds[i].data, nds[i + ].lt);
}
}
cin >> N;
return ;
}
总结:
1、题意:将所给的节点重新排序。 但注意,仅仅是合法节点进行排序。合法节点是指按照第一行所给的首地址能串在一起的所有节点。题目会给孤立的非法节点,需要注意区分。办法就是对合法节点标记1,非法节点标记0。然后利用排序进行分类,以及二级排序。
2、当没有合法节点时,要注意输出特殊情况 0 -1。
3、在读入每一个节点的时候,不要忘记给节点的地址赋值。
A1052. Linked List Sorting的更多相关文章
- PAT A1052 Linked List Sorting (25 分)——链表,排序
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- PAT甲级——A1052 Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- PAT A1052 Linked List Sorting
题意:给出N个结点的地址address.数据域data以及指针域next,然后给出链表的首地址,要求把在这个链表上的结点按data值从小到大输出.样例解释:按照输入,这条链表是这样的(结点格式为[ad ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- Linked List Sorting (链表)
Linked List Sorting (链表) A linked list consists of a series of structures, which are not necessari ...
- PAT1052:Linked List Sorting
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
随机推荐
- 关于树莓派HDMI转VGA线接显示器黑屏
经过数种折腾,找到了解决方法,在SD卡内有个config.txt文件,在其中找到“#hdmi_safe=1”,把#消除掉,变更后成为 # uncomment if you get no picture ...
- Docker容器学习梳理 - 应用程序容器环境部署
关于国内Docker镜像,可以参考:Docker容器学习梳理--基础知识(2) 的Docker镜像使用. 如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等 ...
- 个人阅读作业2:结合《No Silver Bullet》谈谈我在软件开发过程的遇到的困难与体会
英文捉急,只能挑一段看得比较懂的,而且正好和我们现在编程任务联系比较紧密的内容来谈一谈体会. 在<No Silver Bullet>中,作者描述了造成软件本质性困难(essence)的四个 ...
- 软工个人作业-博客作业-WEEK2
1.是否需要代码规范: (1)这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 首先来说,从短期上和个体上来看,一个团队的代码风格必然会在一定程 ...
- Scrum Meeting NO.7
Scrum Meeting No.7 1.会议内容 经过老师提醒,我们认识到,应尽快把主要功能实现,其他的细枝末节应在这之后慢慢添加.当今最重要的任务是和online组和数据处理组实现数据共享. 此外 ...
- 小学生四则运算APP核心代码公布
Mainactivity类: package com.example.XXSCYS; import java.io.ByteArrayOutputStream; import java.io.File ...
- Sprint 冲刺第三阶段第6-10天
这几天一直都在整理我们之前的内容,检查会不会有细节问题.例如界面跳转.颜色等. 因为一直没办法找到guitub存放位置.于是在这里存放一些主代码. MainActivity.java package ...
- 机器学习算法(KNN)
KNN简介 KNN(k-NearestNeighbor)算法的思想总结一下:就是在数据和标签已知的情况下,输入测试数据,将测试数据的特征与训练集中对应的特征进行相互比较,找到训练集中与之最为相似的前K ...
- Mock.js的简单使用
Mock.js 提供的种类有: 步骤: 首先安装:cnpm install mockjs 创建一个mock.js的文件,写好需要引入的数据格式 在main.js中引入mock.js文件: requir ...
- jQuery(四)
get():把jQuery转化成原生js <script> $(function(){ //alert($('#div1').get(0).innerHTML); //jQuery里面也有 ...