A1133. Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10^5) which is the total number of nodes, and a positive K (≤10^3). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer in [-10^5, 10^5], and Next
is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct NODE{
int data, addr, next, valid, greater, neg, rank;
NODE(){
valid = ;
}
}node;
node list[];
int first, N, K;
bool cmp(node a, node b){
if(a.valid != b.valid){
return a.valid > b.valid;
}else{
if(a.neg != b.neg)
return a.neg > b.neg;
else{
if(a.greater != b.greater)
return a.greater < b.greater;
else return a.rank < b.rank;
}
}
}
int main(){
scanf("%d%d%d", &first, &N, &K);
for(int i = ; i < N; i++){
int addr, data, next;
scanf("%d%d%d", &addr, &data, &next);
list[addr].addr = addr;
list[addr].data = data;
list[addr].next = next;
if(data <= K)
list[addr].greater = ;
else list[addr].greater = ;
if(data < )
list[addr].neg = ;
else list[addr].neg = ;
}
int cnt = , pt = first;
while(pt != -){
list[pt].valid = ;
list[pt].rank = cnt;
pt = list[pt].next;
cnt++;
}
sort(list, list + , cmp);
for(int i = ; i < cnt - ; i++){
list[i].next = list[i+].addr;
printf("%05d %d %05d\n", list[i].addr, list[i].data, list[i].next);
}
printf("%05d %d -1\n", list[cnt - ].addr, list[cnt-].data);
cin >> N;
return ;
}
总结:
1、题意:将链表重新排序,要求负数在前,正数在后;同时给出一个正数K,要求小于等于K的数在前,大于K的数在后。至于没有先后关系的数,保持它们在原链表中的先后顺序(注意原链表的顺序不是输入顺序,而是遍历一边之后得到的节点顺序)。
2、做法是使用静态链表,先遍历一遍链表,标注出合法节点。再进行排序,按照合法节点在前,非法节点在后;合法节点中,负数在前;同正负的,小于等于K的数在前。排序后发现似乎不是稳定排序,只好在之前遍历的时候加入一个rank记录每个节点的原始顺序,当两节点之间需要保持原始顺序时使用。
3、在网上还看到一种做法更简便:将所有数分成(负无穷,0),[0,K], (K, 正无穷]三个区间,将不同段的节点依次放入结果数组中即可。
A1133. Splitting A Linked List的更多相关文章
- PAT A1133 Splitting A Linked List (25 分)——链表
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT甲级——A1133 Splitting A Linked List【25】
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...
- PAT1133:Splitting A Linked List
1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- PAT 1133 Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
随机推荐
- day 7-4 互斥锁与队列
一. 基本定义 互斥锁(英语:英语:Mutual exclusion,缩写 Mutex)是一种用于多线程编程中,防止两条线程同时对同一公共资源(比如全局变量)进行读写的机制.该目的通过将代码切片成一个 ...
- 【转】使用 lsof 查找打开的文件
在 UNIX® 环境中,文件无处不在,这便产生了一句格言:“任何事物都是文件”.通过文件不仅仅可以访问常规数据,通常还可以访问网络连接和硬件.在有些情况下,当您使用 ls 请求目录清单时,将出现相 ...
- UML符号
转抄, 语言简练. 挺好. ------------------- ------------------- ------------------- ------------------- -- ...
- vue-cli(vue脚手架)
vue-cli用于自动生成vue+webpack项目. 安装webpack:npm install webpack -g 检查webpack是否安装成功和版本:webpack -v 如果是webpac ...
- 深度学习 weight initialization
转自: https://www.leiphone.com/news/201703/3qMp45aQtbxTdzmK.htmla https://blog.csdn.net/shuzfan/articl ...
- 数据库语法group by
因为在做pgsql和mysql数据库时group by 有报错,但是在以前做mysql5.6的时候没有问题,虽然知道时违反了sql的语法问题,但是没有搞清楚什么原因,也找了不少资料,查找原因,在盆友的 ...
- essential-phone的相关体验
一.adb环境配置 1.下载adb工具 工具网上一搜一大把,注意路径不能有中文. 2.系统配置环境变量 找到环境变量,点击新建.变量名根据自己的习惯随便建,变量值为下载的adb工具解压后存放的路径. ...
- 集成Javascript Logging on MVC or Core
ASP.NET Core provides us a rich Logging APIs which have a set of logger providers including: Console ...
- 初步了解HTML
超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言. 您可以使用 HTML 来建立自己的 WEB 站点,HTML 运行在浏览器上 ...
- [NOIP2017] 宝藏 【树形DP】【状压DP】
题目分析: 这个做法不是最优的,想找最优解请关闭这篇博客. 首先容易想到用$f[i][S][j]$表示点$i$为根,考虑$S$这些点,$i$的深度为$j$情况的答案. 转移如下: $f[i][S][j ...