1133. Splitting A Linked List (25)

简介: Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear ...

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 (<= 105) which is the total number of nodes, and a positive K (<=1000). 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 [-105, 105], 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 <vector>
using namespace std;

struct node{
    int value;
    int next;
} d[100000];
vector<int> v[3];


int main(){
    int n, fadress, k;
    cin >> fadress >> n >> k;
    
    int ad, data, next;
    for (int i = 0; i < n; i++) {
        cin >> ad >> data >> next;
        d[ad].value = data;
        d[ad].next = next;
    }
    
    //直接记录每个节点的顺序 nice
    for (int i = fadress; i != -1; i = d[i].next) {
        if (d[i].value < 0) {
            v[0].push_back(i);
        }else if(d[i].value >= 0 && d[i].value <= k){
            v[1].push_back(i);
        }else{
            v[2].push_back(i);
        }
    }
    
    bool flag = true;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < v[i].size(); j++) {
            if(flag){
                printf("%05d %d ", v[i][j], d[v[i][j]].value);
                flag = false;
            }else{
                printf("%05d\n%05d %d ",v[i][j], v[i][j], d[v[i][j]].value);
            }
        }
    }
    printf("-1\n");
    
    return 0;
}

//22分 又一个测试点过不了
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 题目的意思:and all the values in [0, K] appear before all those greater than K
 地址 n k
 将所有的负数放在前面
 将所有小于k的数字放在k的前面
 其它所有的数的相对位置不改变
 */
struct node{
    int value;
    int next;
} list[100001], ans[100001];

int main() {
    int firstAd, n, k, fa = -1;
    vector<node> v;
    
    cin >> firstAd >> n >> k;
    for (int i = 0; i < n; i++){
        int ad, data, next;
        cin >> ad >> data >> next;
        list[ad].value = data;
        list[ad].next = next;
    }
    
    int p0 = firstAd, p = 0;
    for (int i = 0; i < n; i++) {
        if (list[p0].value < 0) {
            if (fa == -1) {
                fa = p0;
                ans[p0] = list[p0];
                p = p0;
            }else{
                ans[p].next = p0;
                ans[p0] = list[p0];
                p = p0;
            }
        }
        p0 = list[p0].next;
    }
    
    p0 = firstAd;
    for (int i = 0; i < n; i++) {
        if (list[p0].value <= k && list[p0].value >= 0) {
            if (fa == -1) {
                fa = p0;
                ans[p0] = list[p0];
                p = p0;
            }else{
                ans[p].next = p0;
                ans[p0] = list[p0];
                p = p0;
            }
        }
        p0 = list[p0].next;
    }
    
    p0 = firstAd;
    for (int i = 0; i < n; i++) {
        if (list[p0].value > k) {
            if (fa == -1) {
                fa = p0;
                ans[p0] = list[p0];
                p = p0;
            }else{
                ans[p].next = p0;
                ans[p0] = list[p0];
                p = p0;
            }
        }
        p0 = list[p0].next;
    }
    
    for (int i = 0; i < n; i++) {
        if(i != n-1)
            printf("%05d %d %05d\n", fa, ans[fa].value, ans[fa].next);
        else
            printf("%05d %d -1\n", fa, ans[fa].value);
        fa = ans[fa].next;
    }
    
    return 0;
}


目录
相关文章
|
7月前
|
存储 Python
链表(Linked List)详解
链表(Linked List)详解
59 0
|
C++
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
86 0
|
C++
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
76 0
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
88 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
|
存储 C++
线性表的链式存储结构 单链表(Singly Linked List) C++
线性表的链式存储结构 单链表(Singly Linked List) C++
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List