这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发。估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些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. SQL中DateTime转换成Varchar样式

    SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...

  2. eclipse如何汉化,把eclipse改成中文版

    eclipse默认是英文版的,对于中国人来说使用英文语言的软件是件痛苦的事情.下面我来详细说一下如何把eclipse改成中文版的. 工具/原料   eclipse英文版 eclipse中文插件 方法/ ...

  3. 网站架构:PHP针对并发访问如何优化?

    1.拆表:大表拆小表(垂直拆,水平拆:分表,分区partition,分片sharding),可以在应用层实现,也可以在数据库层面实现一部分:提高系统性能. 2.分库:把表放到不同的数据库,这也是分布式 ...

  4. linux系统解决boot空间不足

    有时候更新Linux系统是会碰到boot空间不足的错误,原因基本上是安装时boot空间设置问题可以通过删除旧的内核来释放boot空间. ubuntu: 1.查看当前使用内核版本号       unam ...

  5. 多并发编程基础 之进程 Process

    原贴  https://www.cnblogs.com/gbq-dog/p/10299663.html 1. 进程的理论知识 1.1 操作系统的背景知识 顾名思义,进程即正在执行的一个过程.进程是对正 ...

  6. Python之scrapy框架之post传输数据错误:TypeError: to_bytes must receive a unicode, str or bytes object, got int

    错误名:TypeError: to_bytes must receive a unicode, str or bytes object, got int 错误翻译:类型错误:to_bytes必须接收u ...

  7. jedis 连接redis

    一,  单机版连接 @Test public void testJedis() { //1. 创建jedis 对象 Jedis jedis = new Jedis("192.168.88.1 ...

  8. 认识CSS中字体图标

    前端之HTML,CSS(十一) 字体图标 使用文字做出小图标的效果并超越了小图标应用精灵图,使得图标变得灵活,减少了请求次数,优化了界面的性能.字体图标本身为矢量图. 字体图标的使用过程 1.UI设计 ...

  9. servlet的url-pattern规则

    https://www.cnblogs.com/canger/p/6084846.html

  10. Mac 10.12安装Git管理工具SourceTree

    说明:Git的GUI工具应该是这款最好用. 下载: (链接: https://pan.baidu.com/s/1mhRr35Y 密码: vv67)