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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. PAT_A1133#Splitting A Linked List

    Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...

  5. PAT1133:Splitting A Linked List

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

  6. PAT 1133 Splitting A Linked List[链表][简单]

    1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...

  7. PAT-1133(Splitting A Linked List)vector的应用+链表+思维

    Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...

  8. 1133 Splitting A Linked List

    题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...

  9. PAT 1133 Splitting A Linked List

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

随机推荐

  1. 剑指offer(16)栈的压入、弹出序列

    题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...

  2. Codeforces 1154C Gourmet Cat

    题目链接:http://codeforces.com/problemset/problem/1154/C 题目大意: 主人有一只猫.周一&周四&周日:吃鱼周二&周六:吃兔子周三 ...

  3. DTW的原理及matlab实现

    参考: https://www.cnblogs.com/Daringoo/p/4095508.html

  4. IntelliJ IDEA -- 破解

    ① 到这个地方下载 IntelliJ IDEA 注册码:http://idea.lanyus.com/  就是这个jar包:JetbrainsCrack-2.6.10-release-enc.jar ...

  5. java.lang.Comparable 接口 详解

    参考https://blog.csdn.net/itm_hadf/article/details/7432782 http://www.blogjava.net/jjshcc/archive/2011 ...

  6. CDH 6.0.1 集群搭建 「Process」

    这次搭建我使用的机器 os 是 Centos7.4 RH 系的下面以流的方式纪录搭建过程以及注意事项 Step1: 配置域名相关,因为只有三台机器组集群,所以直接使用了 hosts 的方法: 修改主机 ...

  7. mvn clean compile package install deploy

    (1) package 目的是打包,在pom中,如果是jar就会打包成jar,如果是war就会打包成war 在pom.xml中: <modelVersion></modelVersi ...

  8. python数据结构与算法第五天【顺序表】

    1.列表存储的两种方式 (1)元素内置方式 采用元素内置的方式只能存放同类型元素的数据类型,例如列表中的元素都为整形,元素类型相同,每个元素存放的地址空间大小也相同,则列表中每个元素都是顺序存放的 ( ...

  9. Operation not allowed on a unidirectional dataset错误?

    关于网友提出的“ Operation not allowed on a unidirectional dataset错误?”问题疑问,本网通过在网上对“ Operation not allowed o ...

  10. python之读取和写入csv文件

    写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...