PAT甲级1052、Linked LIst Sorting

news/2025/2/5 10:30:34 标签: 算法, c++

题目

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;
    else
        return 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;
    else
        cout<<"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;
        else
            cout<<setw(5)<<setfill('0')<<node[i+1].addr<<endl;
    }
}

总结

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

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

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

顺带补一句

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

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


http://www.niftyadmin.cn/n/5842151.html

相关文章

为AI聊天工具添加一个知识系统 之76 详细设计之17 正则表达式 之4 正则表达式模板

本文要点 要点 1、三“化” 使用三种不同的定义方法&#xff1a; 规定定义法 -线性回归/内涵定义法--一阶迭代/外延定义法--单调递归 整体形成 一个双人零和 的局面 <Class()外延式, Type()内涵式> Method()规定式。给出 问题“law 是什么”的三种答案&#xff1a; …

Sumatra PDF:小巧免费,满足多样阅读需求

Sumatra PDF是一款完全免费的本地阅读器软件&#xff0c;以小巧的体积和全面的功能受到用户青睐。如今&#xff0c;它已经更新到3.3版本&#xff0c;带来了更多实用功能&#xff0c;尤其是新增的注释功能&#xff0c;值得我们再次关注。 软件特色 轻量级体积&#xff1a;压缩…

蓝桥杯备赛题目练习(一)

一. 口算练习题 ## 题目描述 王老师正在教简单算术运算。细心的王老师收集了 i 道学生经常做错的口算题&#xff0c;并且想整理编写成一份练习。 编排这些题目是一件繁琐的事情&#xff0c;为此他想用计算机程序来提高工作效率。王老师希望尽量减少输入的工作量&#xff0c;比…

vue声明周期及其作用

vue声明周期及其作用 1. 生命周期总览 2. beforeCreate 我们在new Vue()时&#xff0c;初始化一个Vue空的实例对象&#xff0c;此时对象身上只有默认的声明周期函数和事件&#xff0c;此时data,methods都未被初始化 3. created 此时&#xff0c;已经完成数据观测&#xff0…

【算法应用】基于鲸鱼优化算法求解OTSU多阈值图像分割问题

目录 1.鲸鱼优化算法WOA 原理2.OTSU多阈值图像分割模型3.结果展示4.参考文献5.代码获取 1.鲸鱼优化算法WOA 原理 SCI二区|鲸鱼优化算法&#xff08;WOA&#xff09;原理及实现 2.OTSU多阈值图像分割模型 Otsu 算法&#xff08;最大类间方差法&#xff09;设灰度图像有 L L …

宝塔面板端口转发其它端口至MySQL的3306

最近需要把服务器的MySQL服务开放给外网&#xff0c;但又希望公开给所有人。也不想用默认的3306端口。同时也不想改变MySQL的默认端口。 这时候最好的办法就是用一个不常用的端口来转发至3306上去。例如使用49306至3306&#xff0c;外网通过49306来访问&#xff0c;内网依然使用…

使用 sunshine+moonlight 配置串流服务无法使用特殊键

最近了解到串流技术&#xff0c;使用的方案是 sunshine 为串流服务端&#xff0c;moonlight 为客户端&#xff0c;分别在 ipad&#xff0c;android&#xff0c;tv 端安装。 存在的问题 不管说什么平台都会有特殊键无法使用的问题&#xff0c;最初我发现在安卓电视&#xff0c…

【怎么用系列】短视频戒断——对推荐算法进行干扰

如今推荐算法已经渗透到人们生活的方方面面&#xff0c;尤其是抖音等短视频核心就是推荐算法。 【短视频的危害】 1> 会让人变笨&#xff0c;慢慢让人丧失注意力与专注力 2> 让人丧失阅读长文的能力 3> 让人沉浸在一个又一个快感与嗨点当中。当我们刷短视频时&#x…