PAT甲级1052、Linked LIst Sorting

server/2025/2/6 15:26:37/

题目

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 (<10^5) 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 [−10^5,10^5], 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

思路

这个题和1032那个题差不多,都是用到了静态链表,可以总结一下,先说思路吧

明显要用到排序,sort函数比较一下就好了

关键是要重排链表,在输出中next这一部分和输入时是不一样的,幸好不是指针链表,不然只能强行冒泡了

关键点有两个:

1、题目中说的是“有n个节点在内存中”,并不一定有n个节点在链表中

2、n可以为0,又是这个玩意,以后还是默认positive包括0吧

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Node{int addr;int key;int next;bool flag;
}node[100005];bool cmp(Node a,Node b)
{if(a.flag == false || b.flag == false)return a.flag > b.flag;elsereturn a.key < b.key;
}
int main()
{int n,L;cin>>n>>L;for(int i=0;i<100005;i++)node[i].flag = false;for(int i=0;i<n;i++){int add,k,nex;cin>>add>>k>>nex;node[add].addr = add;node[add].key = k;node[add].next = nex;}int cnt = 0;for(int i=L;i!=-1;){node[i].flag = true;cnt++;i=node[i].next;}sort(node, node+100005, cmp);if(cnt)cout<<cnt<<" "<<setw(5)<<setfill('0')<<node[0].addr<<endl;elsecout<<"0 -1"<<endl;for(int i=0;i<cnt;i++){cout<<setw(5)<<setfill('0')<<node[i].addr<<" "<<node[i].key<<" ";if(node[i+1].flag == false)cout<<"-1"<<endl;elsecout<<setw(5)<<setfill('0')<<node[i+1].addr<<endl;}
}

总结

静态链表首先要求总的节点数量(地址)不能太大,这样才能定义一个大数组

然后要求地址不连续,甚至是相当稀疏的

最后是在指针链表不太方便做的时候可以用,比如说大量的交换节点顺序、查询比较信息这些频繁使用链表信息的操作

顺带补一句

在1032那个题中我感觉用静态链表是输入格式的问题,输入的地址是离散的,毫无顺序的,也就是说无法轻易地构建指针链表,所以一开始就分配好空间的静态链表比较合适

所以我感觉看输入格式可能是最有效的方法


http://www.ppmy.cn/server/165438.html

相关文章

基于Ceph14对接openstack的Nova、Glance、Cinder服务为后端存储

openstack对接ceph后端存储 对接glance后端存储对接nova后端存储对接cinder后端存储 openstack T版对接ceph 14版本做glance、nova、cinder后端存储&#xff0c;openstack集群和ceph集群均搭建完毕 节点IPcontroller192.168.200.10compute192.168.200.20storage01192.168.200.3…

手机上运行AI大模型(Deepseek等)

最近deepseek的大火&#xff0c;让大家掀起新一波的本地部署运行大模型的热潮&#xff0c;特别是deepseek有蒸馏的小参数量版本&#xff0c;电脑上就相当方便了&#xff0c;直接ollamaopen-webui这种类似的组合就可以轻松地实现&#xff0c;只要硬件&#xff0c;如显存&#xf…

android 音视频系列引导

音视频这块的知识点自己工作中有用到&#xff0c;一直没有好好做一个总结&#xff0c;原因有客观和主观的。 客观是工作太忙&#xff0c;没有成段时间做总结。 主观自己懒。 趁着这次主动离职拿了n1的钱&#xff0c;休息一下&#xff0c;对自己的人生做一下总结&#xff0c;…

【TensorFlow】T1:实现mnist手写数字识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 1、设置GPU import tensorflow as tf gpus tf.config.list_physical_devices("GPU")if gpus:gpu0 gpus[0]tf.config.experimental.set_memory_g…

crewai框架第三方API使用官方RAG工具(pdf,csv,json)

最近在研究调用官方的工具&#xff0c;但官方文档的说明是在是太少了&#xff0c;后来在一个视频里看到了如何配置&#xff0c;记录一下 以PDF RAG Search工具举例&#xff0c;官方文档对于自定义模型的说明如下&#xff1a; 默认情况下&#xff0c;该工具使用 OpenAI 进行嵌…

Vue 图片引用方式详解:静态资源与动态路径访问

目录 前言1. 引用 public/ 目录2. assets/ 目录3. 远程服务器4. Vue Router 动态访问5. 总结6. 扩展&#xff08;图片不显示&#xff09; 前言 &#x1f91f; 找工作&#xff0c;来万码优才&#xff1a;&#x1f449; #小程序://万码优才/r6rqmzDaXpYkJZF 在 Vue 开发中&#x…

糖果(安师大)

转移方程 转移方程的核心思想是 选择和不选择当前物品 两种情况的比较。具体来说&#xff1a; 不选择当前物品&#xff1a; 如果不选择第 i 个物品&#xff0c;那么 dp(i, j) 就等于 dp(i-1, j)&#xff0c;即前 i-1 个物品中&#xff0c;满足 总价值 % k j 的最大和。 选…

河洛理数【陈抟】同年月日时生的分辨

相信大家和我都有一个疑问&#xff1a;就是同年月日时出生的人比比皆是&#xff0c;但是这些人八字虽相同&#xff0c;而贫贱富贵却相差很大&#xff0c;那是什么原因导致得&#xff1f;此篇章节陈抟给出了说明。 首先针对原来存在的两点论调陈抟进行了证伪。第一点就是方位不同…