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 ...
随机推荐
- Spring学习日志之纯Java配置的MVC框架搭建
依赖引入 <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifa ...
- C#使用FFMPEG推流,并且获取流保存在本地,随时取媒体进行播放!
最近开发了基于C#的推流器一直不大理想,终于在不懈努力之后研究了一点成果,这边做个笔记:本文着重在于讲解下如何使用ffmpeg进行简单的推流,看似简单几行代码没有官方的文档很吃力.并获取流的源代码:如 ...
- oracle数据恢复方法
https://www.cnblogs.com/hqbhonker/p/3977200.html
- 如何启动Intel VT-X及合理利用搜索
昨天安装Vmware的时候不幸遇到了Vt-X被禁用的麻烦,上网百度了一下才知道要进入Bois进行设置.说起百度就不得不提到模糊搜索这个概念.这个特性的优点和缺点可谓同等突出,有了模糊搜索大家可以在信息 ...
- Visual Studio 2015的安装及单元测试练习
第一部分:Visual Studio 2015的安装 我电脑系统是win10,所以安装的是Visual Studio 2015,安装步骤部分截图如图所示: 1.安装类型选项界面:可以选择默认安装,可以 ...
- SCRUM 12.22
周一,大家现在课程也比较少,今天都在非常努力地写代码. 任务分配如往常一样,我们现在基本将工作的重心放在完善已有的组件上. 成员 任务 彭林江 落实API 牛强 落实意见反馈功能测试 高雅智 测试已完 ...
- 《Linux内核设计与实现》第3章读书整理
第三章.进程管理 3.1进程 1.进程就是处于执行期的程序,但进程并不仅仅局限于一段可执行程序代码 2.执行线程: 简称线程,是在进程中活动的对象.每个线程都拥有一个独立的程序计数器.进程栈和一组进程 ...
- sqoop 使用笔记
好久没有更新自己技术博客,现在开始工作了,把自己遇到的问题写到这里边来 主要把自己的问题写出来,分享给大家 sqoop 导入数据时候 有时候会遇到mysql 中有sql 中的关键字 这时候如果直接导出 ...
- 最终版alpha阶段总结
这是我们组最终的alpha阶段总结,我和陈汝婷虽然最后做的没有想象的好,时间也很紧急,但是真的学到很多,毕竟现在我们两个人做的活是其他组四个人做的活,其实能做到这样,哪怕这样,我们也觉得没有什么遗憾了 ...
- 定义类型别名(typedef,using)
说到类型别名,无非是给类型名(如int,char,float,double,bool)取一个比较有特殊含义的名字而已 最常用的关键莫过于 typedef 吧 typedef最常见的用法是与结构体str ...