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
题目要求把链表排序,如果只有一个链表,排序过程中势必会改变连接关系,从而造成循环出错,因此应该至少有两个链表才能进行排序。
这里用另一种方法,把链表放入vector排序,然后输出,这样就可以利用系统的排序函数,需要注意的是题目给出的可能是多个链表,因此应该从头开始向后遍历到-1,处理这个链,而忽略其他部分。
我按照这个思路,最后一个case总是内存超限,比较了网上的代码也没发现自己的问题所在,后来参考了sunbaigui的解法,思路一致,但是他这个代码可以把内存控制在10M以内,代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h> #define MAX 1000000 using namespace std; typedef struct Node
{
int me, key, next;
bool operator < (const Node& n) const
{
return key < n.key;
}
}Node; int main()
{
int n, root;
scanf("%d%d",&n,&root);
vector<Node> nodeMap(MAX);
for (int i = 0; i < n; ++i)
{
Node node;
scanf("%d%d%d",&node.me, &node.key, &node.next);
if(node.me >= 0 && node.me < MAX)
nodeMap[node.me] = node;
} vector<Node> nodeList;
int cur = root;
while(cur >= 0 && cur < MAX)
{
nodeList.push_back(nodeMap[cur]);
cur = nodeMap[cur].next;
} if (nodeList.empty())
{
if (root == -1)
printf("0 -1\n");
else printf("0 %05d\n",root);
}
else
{
sort(nodeList.begin(), nodeList.end());
printf("%d %05d\n", nodeList.size(), nodeList[0].me);
for (int i = 0; i < nodeList.size(); ++i)
{
if(i != nodeList.size()-1)
printf("%05d %d %05d\n", nodeList[i].me, nodeList[i].key, nodeList[i+1].me);
else printf("%05d %d -1\n", nodeList[i].me, nodeList[i].key);
}
}
return 0;
}
1052. Linked List Sorting (25)的更多相关文章
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- 【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甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- 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 (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 1052 Linked List Sorting (25分)
题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
随机推荐
- python 中range numpy.arange 和 numpy.linspace 的区别
1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...
- C语言程序设计第四次作业--选择结构(2)
(一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...
- Vue2学习(3)
子组件索引 尽管有 props 和 events,但是有时仍然需要在 JavaScript 中直接访问子组件.为此可以使用 ref 为子组件指定一个索引 ID.例如: <div id=" ...
- js登录,回车登录
$(document).ready(function(){ $("#loginBtn").click(doLoginEvent); loadCookies(); //回车登录 do ...
- TypeScript: Week Reflection
TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...
- js error
0x800a0259 - JavaScript 运行时错误: 未知的运行时错误 <p id="navigatorInfo"></p> var txt = & ...
- Linux服务器之间免密登录设置
说明: A为linux服务器a B为linux服务器b 每台linux都有ssh的服务端和客户端,linux下的ssh命令就是一个客户端 我们常用ssh协议来进行登陆或者是文件的拷贝,都需要密码 A要 ...
- ubuntu14.04 安装PIL库出现OError: decoder jpeg not available 的解决方案
出现 OError: decoder jpeg not available 的原因是,没有装JPEG的库,同时要支持png图片的话还要装 ZLIB.FREETYPE2.LITTLECMS的库文件. 先 ...
- java基础复习+大数运算
String: Array: 下面分别是大数加法,加法,乘法,取模
- Node.js Buffer(缓冲区)
JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...