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),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
随机推荐
- win7+Apache 设置域名指向本地文件夹
实现:浏览器地址栏输入 www.bnzoo.com 访问 D:/www 系统:win7旗舰版+Apache 步骤: 1.打开文件 C:\Windows\System32\drivers\etc\hos ...
- Unique-paths (动态规划)
题目描述 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...
- 使用JdbcTemplate 操作PostgreSQL,当where条件中有timestamp类型时,报错operator does not exist: timestamp w/out timezone
今天遇到一个问题,找了还半天,Google一下,官网显示是一个bug. 思考一番肯定是类型出了问题. Controller: Service:转化时间戳 Dao: 一波转换搞定!
- eclipse中安装freemarker插件及ftl使用freemarker编辑器
http://www.07net01.com/2015/08/895212.html eclipse中安装freemarker插件及ftl使用freemarker编辑器 在线安装的方法是:Help – ...
- 索引法则--IS NULL, IS NOT NULL 也无法使用索引
Mysql 系列文章主页 =============== 1 数据准备 1.1 建表 DROP TABLE IF EXISTS staff; CREATE TABLE IF NOT EXISTS st ...
- mongoose多条件模糊查询实例
mongoose多条件模糊查询 这是今天手头项目中遇到的一个问题,关于mongoose如何实现类似于SQL中 nick LIKE '%keyword%' or email LIKE '%keyword ...
- 日常实用css布局技巧汇总
1.单行完整显示,多行省略显示. .box { width: 100px; //必要 display: -webkit-box; //必要 font-size: 14px; line-heig ...
- ubuntu查看IO
在命令行直接 cp 一个比较大的文件时,由于没有提示信息,总感觉很不放心,可以通过查看IO的方式确认cp操作的进展程度. 查看IO可以使用iostat命令,但是前提是要安装sysstat sudo a ...
- C++/C# 开发高级案例资料一次送!关注加群领取哦!
目前C/C++.C#.JAVA等语言开发的在线教育比较火爆,但小编所见的讲解高级案例的非常少,切合市场需求的较少.而且针对铁路.公路.建筑.市政.制造业等所有工业计算机辅助设计的开发技术和案例几乎没有 ...
- JS的replace默认只替换第一个匹配项
1. JS的replace默认只替换第一个匹配项. 解决方法: 使用正则表达式进行匹配替换[ ①.replace(new RegExp(②,"g") ,③); ] ①:包含 ...