【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 necessarily adjacent in memory. We assume that each structure contains an integer key and a Nextpointer 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
C++代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std; #define maxn 100010 struct Node {
int address;
int key;
int next;
bool flag = false;
}node[maxn]; bool cmp(const Node &a, const Node &b) {
if (a.flag == false || b.flag == false)
return a.flag > b.flag;
else
return a.key < b.key;
} int main() {
int n, add;
scanf("%d %d", &n, &add);
int begin;
for(int i=;i<n;i++){
scanf("%d", &begin);
scanf("%d %d", &node[begin].key, &node[begin].next);
node[begin].address = begin;
}
int cont=,p=add;
while (p != -) {
cont++;
node[p].flag = true;
p = node[p].next;
}
sort(node, node + maxn, cmp);
if (cont == ) {
printf("0 -1\n");
return ;
}
printf("%d %05d\n", cont, node[].address);
for (int i = ; i < cont-; i++) {
printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+].address);
}
printf("%05d %d -1\n", node[cont - ].address, node[cont - ].key);
return ;
}
【PAT】1052 Linked List Sorting (25)(25 分)的更多相关文章
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT A1052 Linked List Sorting (25 分)——链表,排序
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- PAT 1052. Linked List Sorting
这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发.估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过 #include <iost ...
- 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)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- 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 ...
随机推荐
- MT【75】考察高斯函数的一道高考压轴题
解答:答案1,3,4. 这里关于高斯函数$[x]$的一个不等式是需要知道的$x-1<[x]\le x$,具体的:
- 【BZOJ1021】[SHOI2008]循环的债务(动态规划)
[BZOJ1021][SHOI2008]循环的债务(动态规划) 题面 BZOJ 洛谷 题解 感觉以前的题目都好小清新啊,我这种智商丢失的选手完全写不动. 这题看着就像一个\(dp\),并且我们发现每种 ...
- 【agc002f】Leftmost Ball(动态规划)
[agc002f]Leftmost Ball(动态规划) 题面 atcoder 洛谷 题解 我们从前往后依次把每个颜色按顺序来放,那么如果当前放的是某种颜色的第一个球,那么放的就会变成\(0\)号颜色 ...
- Python 通过gevent实现协程
#coding:utf-8-*- '''协程(coroutine)又称微线程.纤程,是一种用户级的轻量级线程.协程有自己的寄存器上下文和栈.携程调度时,将寄存器上下文和栈 保存,在切换回来的时候恢复保 ...
- sql知识收集
在SQL Server里面有top关键字可以很方便的取出前N条记录,但是Oracle里面却没有top的使用,类似实现取出前N条记录的简单方法如下: 方法1:利用ROW_NUMBER函数 取出前5条记录 ...
- isspace 对含有中文 的字符串进行检查的时候表现不正常!?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
- 1: mysql left join,right join,inner join用法分析
下面是例子分析表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 ...
- SSM搭建Spring单元测试环境
原文链接:https://jingyan.baidu.com/article/93f9803f5a97a4e0e46f55c8.html SSM搭建Spring单元测试环境
- DNS ------ windows执行DNS过程
前言 TCP/IP协议是网络通讯的基础,平常在对TCP/IP协议设置时会涉及到IP地址,子网掩码,默认网关和DNS这几个参数.这里我们主要谈下IP地址和DNS这两个参数之间的关系. 首先计算机在网络中 ...
- 枚举类型---java基础代码
package com.mon11.day4; /** * 类说明 :定义枚举 * @author 作者 : chenyanlong * @version 创建时间:2017年11月4日 */ pub ...