PAT 1052. Linked List Sorting
这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发。估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些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的更多相关文章
- 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 解题报告 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)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- 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
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
- 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 ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- 【FAQ】tomcat启动jdk版本不一致
一.tomcat7.exe与startup.bat的区别: 1.这两个都可以启动tomcat,但tomcat7.exe必须安装了服务才能启动,而startup.bat不需要 2.另外一个区别是它们启动 ...
- [SQL] 简单新建(create)删除(drop\delete)权限(grant/revoke)修改(set\update)
一.前言 说起来 数据库(Structured Query Language),本站写过很多类似文章. 如: Mysql创建.删除用户 phpMyAdmin 登陆需要密码 记一次裸迁 MySQL 经历 ...
- Linux 中排除掉筛选的文件
以下命令以网站目录www为例做介绍,有时候更新网站的时候需要保留比如图片目录,或者其他目录就需要这样的操作 实例一: 删除文件夹内所有文件只保留一个文件命令 [root@linuxzgf www]# ...
- pandas.concat连接dataframe
https://blog.csdn.net/stevenkwong/article/details/52528616
- Java性能优化技巧及实战
关于Java代码的性能优化,是每个javaer都渴望掌握的本领,进而晋升为大牛的必经之路,但是对java的调优需要了解整个java的运行机制及底层调用细节,需要多看多读多写多试,并非一朝一夕之功.本文 ...
- 版本控制(.git + .svn)
git 分布式版本控制系统 底层C语言 按元数据方式存储,采用SHA-1哈希算法(内容完整性好) 结合GitHub,为开源项目免费提供Git存储 git config --global user.na ...
- JQ 文件上传
var formData = new FormData(); var name = $("input").val(); formData.append("file&quo ...
- 网络基础 05_DHCP
1 DHCP概述 DHCP (Dynamic Host Configuration Protocol)是一种动态的向Internet终端提供配置参数的协议.在终端提出申请之后,DHCP可以向终端提供I ...
- 图像的上采样(upsampling)与下采样(subsampled)
缩小图像(或称为下采样(subsampled)或降采样(downsampled))的主要目的有两个:1.使得图像符合显示区域的大小:2.生成对应图像的缩略图. 放大图像(或称为上采样(upsampli ...
- Q147 对链表进行插入排序
插入排序的动画演示如上.从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中. 插入排序算法: 插入排序 ...