PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25)
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
题目描述:
给一个链表按照node里面存着的key排序.
算法分析:
算法很简单就是排序算法, 调用现成的sort之类的库就行了, 注意两点”
(1) 给定的node不一定都是在同一个链表上 (这就是为什么我们需要head node 的address)
(2) head node address 可能为-1,小心segment fault.
(3) 如果链表是空,应当输出”0 -1″.
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
#define MX 100001
struct Node {
int key, val, next;
};
//不用vector,而用数组的好处是,数组可作为hash
Node m[MX], linked[MX];
bool cmp(Node p, Node q) {
return p.val<q.val;
} int main()
{
int N, head;
scanf("%d%d", &N, &head);
for (int i=; i<N; i++) {
int tmp;
scanf("%d", &tmp);
scanf("%d%d", &m[tmp].val, &m[tmp].next);
m[tmp].key = tmp;
}
int cnt=;
while (head != -) {
linked[cnt++] = m[head];
head = m[head].next;
} sort(linked, linked+cnt, cmp);
if (cnt == ) printf("0 -1");
else {
printf("%d %05d\n", cnt, linked[].key);
for (int i=; i<cnt;i++) {
if (i!=cnt-) {
printf("%05d %d %05d\n", linked[i].key, linked[i].val, linked[i+].key);
}
else {
printf("%05d %d -1", linked[i].key, linked[i].val);
}
}
} return ;
}
PAT 解题报告 1052. Linked List Sorting (25)的更多相关文章
- PAT (Advanced Level) 1052. Linked List Sorting (25)
		
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
 - 【PAT甲级】1052 Linked List Sorting (25 分)
		
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
 - 【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 (25)
		
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
 - PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
		
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
 - PAT Advanced 1052  Linked List Sorting (25) [链表]
		
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
 - PAT甲题题解-1052. Linked List Sorting (25)-排序
		
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
 - PAT 解题报告 1013. Battle Over Cities (25)
		
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
 - 1052. Linked List Sorting (25)
		
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
 
随机推荐
- thinkphp开发技巧经验分享
			
thinkphp开发技巧经验分享 www.111cn.net 编辑:flyfox 来源:转载 这里我给大家总结一个朋友学习thinkphp时的一些笔记了,从变量到内置模板引擎及系统变量等等的笔记了,同 ...
 - PHP面向对象程序设计的61条黄金法则
			
PHP面向对象程序设计的61条黄金法则 你不必严格遵守这些原则,违背它们也不会被处以宗教刑罚.但你应当把这些原则看成警铃,若违背了其中的一条,那么警铃就会响起 . ----- Arthur J.R ...
 - 20145317彭垚 《Java程序设计》第4周学习总结
			
20145317彭垚 <Java程序设计>第04周学习总结 20145317彭垚 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承就是避免多个类间重复定义共同行 ...
 - ArcGIS Server发布服务,打包成功,发布失败
			
打包成功,发布失败 部分解决方案: ① 查看Server对于源数据所在文件夹是否有读写权限,若无赋予Server账户至少读写权限.读写权限的赋予:对应存放数据的文件夹上右键→属性→ 安全 赋予ar ...
 - Lazarus -Pascal常量
			
2.常量 2.1.普通常量 仅仅下面类型可以被定义为常量Ordinal类型Set类型指针类型 (but the only allowed value is Nil). real类型 Char, Str ...
 - 十 	mybatis逆向工程
			
1 逆向工程 1.1 什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper. ...
 - ext在web工程目录导致myeclipse内存溢出问题
			
分类: Extjs2013-01-24 00:01 2068人阅读 评论(2) 收藏 举报 当在eclipse中的web工程中增加了extjs4,出现An internal error occurre ...
 - linux 查看系统状态方法
			
Linux下如何查看系统启动时间和运行时间 1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.0 ...
 - SHELL 八大扩展
			
最近在梳理bash知识的的过程中,有幸阅读了man bash文档,一时间犹如醍醐灌顶一般,很多当初不明白的地方都豁然开朗,现在就其中的一点做一分享,同时也为man bash做一下广告,当你面对bash ...
 - CLH锁 、MCS锁
			
一.引文 1.1 SMP(Symmetric Multi-Processor) 对称多处理器结构,指服务器中多个CPU对称工作,每个CPU访问内存地址所需时间相同.其主要特征是共享,包含对CPU,内存 ...