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),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
随机推荐
- UDA机器学习基础—交叉验证
交叉验证的目的是为了有在训练集中有更多的数据点,以获得最佳的学习效果,同时也希望有跟多的测试集数据来获得最佳验证.交叉验证的要点是将训练数据平分到k个容器中,在k折交叉验证中,将运行k次单独的试验,每 ...
- python中的缩进问题
python中没有{}来表示代码块,而是用缩进来表示,刚开始写python代码,没有注意缩进,结果各种报错(( ╯□╰ )). 在python中的原则就是同一层次的代码一定要有相同的缩进!!! 从上图 ...
- 如何避免 async/await 地狱
简评:async/await 写着很爽,不过要注意这些问题. async/await 让我们摆脱了回调地狱,但是这又引入了 async/await 地狱的问题. 什么是 async/await 地狱 ...
- easyui datagrid 排序问题
$('#dg').datagrid({ remoteSort:false,④ sortName:'sysfield', ① sortOrder:'desc',② columns:[[ {field ...
- map内置函数、lambda表达式、快捷生成想要的列表、filter内置函数
map函数 语法 map(function, iterable, ...) 参数 function -- 函数,有两个参数 iterable ...
- mybatis自动生成
很简单只要配两个文件 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- Linux(centos7)下安装Docker
近期公司开始推Docker技术.这个系列的文章都是基于CentOS7系统下进行讲解的. Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器 ...
- API说明书规范
目录 1 前言 1.1 编写目的 1.2 预期读者 1.3 关于API设计开发 2 API公共说明 3 文档API索引 ...
- CSS相关
===CSS框架=== https://github.com/lucasgruwez/waffle-grid 一个易于使用的 flexbox 栅格布局系统 ===CSS初始化=== https://g ...
- jQuery – AJAX load() 方法
jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...