这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发。估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些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. 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析

    00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...

  3. Linux之Ubuntu系统安装搜狗输入法

    如何在Ubuntu系统中安装搜狗输入法? 1.第一步  下载搜狗输入法文件for Linux 2.检查更新 update 如果没有更新的话,需要做这一步 3.语言支持 选择fcitx,然后关闭界面 4 ...

  4. 可以修改类不用重启Tomcat加载整个项目

    修改类不重启Tomcat(不用手动重启) 修改tomcat  conf目录下的server.xml <Context path="/struts2" docBase=&quo ...

  5. GeneXus学习笔记——入门篇

    使用GeneXus做开发做了有一段时间了 却发现一个问题(O_O)?就是除了相关的Wiki外 网上其他地方的相关资料都很少 于是乎我就想在这记录一些东西 来帮助以后会用到的人(°ー°") 那 ...

  6. Mac下Go2Shell配合ITerm2无法定位到当前文件夹目录的解决方法

    下载最新版,这个问题在最新版已经完美解决. http://zipzapmac.com/go2shell

  7. Mac下的裁剪快捷键

    按[Command]+[C]复制文件,然后按[Command]+[Option]+[V]. 注意:[Command]+[X]只能剪切文字文本.

  8. Mac下安装tomcat8(Mac 10.12)

    1.到官网下载tomcat8 http://tomcat.apache.org/download-80.cgi 说明:tomcat最好不要下载最新的,选择一个适中的最好. 2.安装 ▲解压并重命名文件 ...

  9. 用python开发了一个简单apache web服务端范例,在win10 + apache2.4.9 + python3.5 测试成功

    #!D:\Programs\Python\Python35-32\python.exe import cgi def htmlTop():     print("Content-type: ...

  10. JDBC处理可滚动的处理集

    Statement createStatement(int resultSetType,                           int resultSetConcurrency,     ...