题目如下:

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)的更多相关文章

  1. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  2. 【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 ...

  3. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  4. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  5. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  6. PAT Advanced 1052 Linked List Sorting (25) [链表]

    题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...

  7. PAT (Advanced Level) 1052. Linked List Sorting (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 1052 Linked List Sorting (25分)

    题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...

  9. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

随机推荐

  1. PHP查看本地文件夹及删除文件夹操作

    查看文件夹(包括文件夹内所有的文件夹和文件) function descdir($dir){ if(is_dir($dir)){ if($dh=opendir($dir)){ while(($file ...

  2. python学习之装饰器-

    python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...

  3. java.sql.SQLException: **** [SQLServer]对象名 "XXXX"无效

    原因:进到数据库里面,但是没有选择数据库. 方法:检查数据源配置,这玩意容易看出.难得是多数据库操作时,切换数据源!

  4. c++指针函数的使用——回调函数

    /* 函数指针 函数也是有地址的 所谓函数指针,就是指向函数的指针,函数指针也是一个变量,可以指向不同的函数.同时通过函数指针可以调用其指向函数,从而使函数的调用更加灵活. 函数指针的用途 */ #i ...

  5. delphi 验证码识别(XE8源码)

    如题:源码下载

  6. 05_CRUD操作

      1.Params拦截器: 作用:Parameters拦截器将把表单字段映射到ValueStack栈的栈顶对象的各个属性中, 注意:如果某个字段在栈顶对象中没有对应的属性,则Params拦截器将尝试 ...

  7. VSCode 插件推荐

    vscode-icons  用于项目中文件类型显示对应的图标,提高文件定位的效率. vscode-tslint  用于 TS 的规范检测 Path Intellisense  用于提示导入文件时候的路 ...

  8. ReactNative 4Android源码分析二: 《JNI智能指针之实现篇》

    文/Tamic http://blog.csdn.net/sk719887916/article/details/53462268 回顾 上一篇介绍了<ReactNative4Android源码 ...

  9. Rails里rake db:migrate出现undefined method last_comment问题的解决

    这个问题和特定的rake版本有关,因为Rails要使用rake的last_comment方法在较新版本的rake中已被废弃,所以很多人卸载了新版本的rake去安装旧版本的rake. 这样也能解决问题, ...

  10. Django Model field reference

    ===================== Model field reference ===================== .. module:: django.db.models.field ...