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 ...
随机推荐
- http指南(2)--代理
代理 单个客户端专用的代理称为私有代理,众多客户端共享的代理被称为公共代理 代理与网关的对比:代理连接的是两个或多个使用相同协议的应用程序,而网关连接的则是两个或多个使用不同协议的端点.网关扮演的是“ ...
- ExtJs 编译
前台使用Extjs加载源码的话是非常庞大的,编译之后就只加载一个app.js文件.这种技能如果不知道的话怕别人骂我不是个女程序员.哈哈哈哈哈. 打开cmd,进入程序Extjs的文件夹,如我的程序Ext ...
- DRBD详细解说及配置过程记录
一.DRBD介绍 DRBD(Distributed ReplicatedBlock Device)是一种基于软件的,无共享,分布式块设备复制的存储解决方案,在服务器之间的对块设备(硬盘,分区,逻辑卷等 ...
- 分布式监控系统Zabbix-3.0.3-完整安装记录 - 添加shell脚本监控
对公司的jira访问状态进行监控,当访问状态返回值是200的时候,脚本执行结果为1:其他访问状态返回值,脚本执行结果是0.然后将该脚本放在zabbix进行监控,当非200状态时发出报警.jira访问状 ...
- Command Analyze failed with a nonzero exit code
在运行RN项目的时候,报 Command Analyze failed with a nonzero exit code ,试着将build System 修改下
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration
我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...
- 个人对vuex的表象理解(笔记)
一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...
- C语言与汇编衔接1
研究实验二 问题研究过程: 发问:C语言中的变量究竟是什么,通过下面的程序进行C语言中的变量的学习 图1 URL.EXE函数 为了研究main函数的首地址,我首先自作聪明的用了一条_DX=main, ...
- 20135337——Linux实践三:ELF文件格式(64位系统,简单分析)
ELF文件格式简单分析 (具体分析见上一篇ELF文件格式32位系统) ELF-header 第一行: 457f 464c :魔数: 0201 :64位系统,小端法 01 :文件头版本 剩余默认0: 第 ...
- 同步手绘板——android端下笔后颜色变化
实现效果 : 在设计之初我们以为是改变笔线条的粗细来实现类似效果,后来通过找其他相关软件比对发现是不改变线条的粗细,通过改变透明度实现下笔后颜色的渐变,虽然已实现,但渐变效果很一般,不流畅,算法还待优 ...