PAT 1133 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<vector>
using namespace std;
struct node{
    int val;
    int next;
};
int main(){
    int First, n, k, t=-1, cnt=0;
    cin>>First>>n>>k;
    vector<node> data(100000);
    vector<int> negative, posl, posr, ans;
    for(int i=0; i<n; i++){
        int d, v, next;
        cin>>d;
        cin>>data[d].val>>data[d].next;
    }
    while(First!=-1){
        if(data[First].val<0)
            negative.push_back(First);
        else if(data[First].val<=k)
            posl.push_back(First);
        else if(data[First].val>k)
            posr.push_back(First);
        First=data[First].next;
    }
    for(int i=0; i<negative.size(); i++)
        ans.push_back(negative[i]);
    for(int i=0; i<posl.size(); i++)
        ans.push_back(posl[i]);
    for(int i=0; i<posr.size(); i++)
        ans.push_back(posr[i]);
    for(int i=0; i<ans.size(); i++)
        if(i!=ans.size()-1)
            printf("%05d %d %05d\n", ans[i], data[ans[i]].val, ans[i+1]);
        else
            printf("%05d %d -1", ans[i], data[ans[i]].val);
    return 0;
} 

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9506883.html

时间: 2024-10-09 16:22:41

PAT 1133 Splitting A Linked List的相关文章

1133 Splitting A Linked List (25 分)

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 before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. Th

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

PAT1133:Splitting A Linked List

1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

PAT (Advanced Level) 1052. Linked List Sorting (25)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace std;

【PAT】2-1 Reversing Linked List

题目地址:2-1 按给定的K个间隔翻转链表.给出了链表的首地址和结点个数以及间隔K,每个结点又提供了自身的地址.存储的数值以及下一个结点的地址.结点构造成一个结构体,所有结点放在结构体数组里,其中注意存储的技巧——将地址作为数组的数值下标,而数组值是数据内容以及下一个节点的地址.同时注意存在无效的结点. 手残感觉输出的时候好麻烦. #include <iostream> #include <algorithm> #include <cstdlib> #include &

【PAT甲级】1052 Linked List Sorting (25 分)

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树

1132 Cut Integer(20 分) 题意:将一个含K(K为偶数)个数字的整数Z割分为A和B两部分,若Z能被A*B整除,则输出Yes,否则输出No. 分析:当A*B为0的时候,不能被Z整除,输出No.否则会出现浮点错误. #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<map>

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10