这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发。估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过

#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map> using namespace std; class Node {
public:
int data;
int next;
Node() : data(), next(-){
cout<<"should not happend"<<endl;
}
Node(int d, int n) : data(d), next(n) {}
}; int count(int head, unordered_map<int, Node>& mem) {
int cur = head;
int cnt = ;
while (cur != -) {
cnt++;
cur = mem[cur].next;
}
return cnt; } int step(int head, int k, unordered_map<int, Node>& mem) {
int cur = head;
while (cur != -) {
if (k-- == ) {
break; }
cur = mem[cur].next;
}
return cur;
} int merge_list(int heada, int headb, unordered_map<int, Node>& mem) {
int nhead = -;
int last = -;
int select = -;
int ca = heada, cb = headb;
while (ca != - && cb != -) {
Node& na = mem[ca];
Node& nb = mem[cb]; if (na.data > nb.data) {
select = cb;
cb = nb.next;
} else if (na.data <= nb.data) {
select = ca;
ca = na.next;
} if (last == -) {
nhead = select;
} else {
mem[last].next = select;
}
last = select;
} int last_part = -; if (ca != -) {
last_part = ca;
} if (cb != -) {
last_part = cb;
} if (last == -) {
nhead = last_part;
} else {
mem[last].next = last_part;
} return nhead;
} int sort_list(int head, int n, unordered_map<int, Node>& mem) { if (n < ) {
return -;
} if (n == ) {
mem[head].next = -;
return head;
} int a_cnt = n / ;
int b_cnt = n - a_cnt; int ca = head;
int cb = step(head, a_cnt, mem); ca = sort_list(ca, a_cnt, mem);
cb = sort_list(cb, b_cnt, mem); return merge_list(ca, cb, mem); } void print_list(int head, unordered_map<int, Node>& mem) {
int cur = head;
while (cur != -) {
Node& cn = mem[cur];
if (cn.next == -) {
printf("%05d %d %d\n", cur, cn.data, cn.next);
} else {
printf("%05d %d %05d\n", cur, cn.data, cn.next);
}
cur = mem[cur].next;
}
} int main() { int N, head; scanf("%d%d", &N, &head); unordered_map<int, Node> mem; for (int i=; i<N; i++) {
int addr, data, next;
scanf("%d%d%d", &addr, &data, &next);
mem.insert(make_pair(addr, Node(data, next)));
} int n = count(head, mem); head = sort_list(head, n, mem);
if (n > ) {
printf("%d %05d\n", n, head);
} else {
printf("%d %d\n", n, head);
}
print_list(head, mem); return ; }

PAT 1052. Linked List Sorting的更多相关文章

  1. PAT 1052 Linked List Sorting [一般]

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

  2. Pat 1052 Linked List Sorting (25)

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

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

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

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

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

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

  6. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

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

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

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

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

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

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

随机推荐

  1. Microsoft Windows XP Professional X64 Edition Corporate Keys

    FVMK4-6DD4B-26MB4-74JB2-R4XWM DHR8W-69GX3-YWPM9-P98K2-B2V4Y DDR6D-XMQ6V-78Y2B-B6TP4-YXMRY J4K6H-DTTF ...

  2. Spring Boot入门教程(1)

    Spring Boot入门教程(1) 本文将使用Spring Boot一步步搭建一个简单的Web项目来帮助你快速上手. 将要用到的工具 JDK 8 IntelliJ IDEA(Ultimate Edi ...

  3. OKR 第一阶段

    性能优化,主要是为了提高用户体验. 1.  根据浏览器的工作原理,dom树解析时,遇到css 以及js 会出现阻塞,为了缩短dom树解析时间,进行了js  增加 async 的异步加载过程 . 原有代 ...

  4. 2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I

    1117 AlvinZH's Fight with DDLs I 思路 简单题,动态规划. 本题与期末练习赛B题很相似,而且更为简单些.简化问题:在数字序列上取数,不能取相邻的数. DP数组定义,dp ...

  5. C#-WebForm-光棒效果

    <script type="text/javascript"> //获取Repeater的每一行 var oItems = document.getElementsBy ...

  6. Java中静态变量的声明位置

    Java中静态变量只能是成员变量,局部方法中的局部变量除final外不能有任何其他修饰符,例如: public class Test { static String x = "1" ...

  7. 云服务器、vps、虚拟主机的区别

    云服务器 Elastic Compute Service, 简称ECS 好多人理解云服务器和VPS一样,更有甚者说以前的VPS现在的说法就是云服务器,其实不然,云服务器是一个计算,网络,存储的组合.简 ...

  8. Linus' Law

    Given enough eyeballs, all bugs are shallow.                                               ------埃里克 ...

  9. 查看tomcat部署的项目名

    Myeclips的查看方法 项目名右键-->properties-->Myeclips-->deployment 这里虽然可以改这个路径的项目名 但是一般不建议更改 避免出现未知错误 ...

  10. List的定制排序 包括使用lambda表达式来实现的方法

    1.先实现Comparator的接口 重写compare方法 根据比较大小来返回数值: 比如:(Integer o1  -   Integer o2); return 1 表示o1>o2; re ...