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

PAT 1133 Splitting A Linked List的更多相关文章

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

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

  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. 1133 Splitting A Linked List

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

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

  5. PAT1133:Splitting A Linked List

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

  6. PAT_A1133#Splitting A Linked List

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

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

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

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

  9. PAT甲级——A1074 Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

随机推荐

  1. mybaties中,模糊查询的几种写法

    模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...

  2. springmvc 用fasterxml.jackson返回son数据

    一,引入fasterxm.jackson包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> & ...

  3. java笔记线程方式1等待终止

    public final void join():等待该线程终止 public class ThreadJoinDemo { public static void main(String[] args ...

  4. ASP.Net 下载大文件的实现 (转)

    原文:http://www.cnblogs.com/luisliu/p/4253815.html 当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃 ...

  5. [Swift]通天遁地Swift

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. 洛谷 P3389 【模板】高斯消元法

    以下这个好像叫高斯约旦消元法,没有回代 https://www.luogu.org/blog/37781/solution-p3389 #include<cstdio> #include& ...

  7. ACM_Appleman and Card Game(简单贪心)

    Appleman and Card Game Time Limit: 2000/1000ms (Java/Others) Problem Description: Appleman has n car ...

  8. List 的属性与方法整理

    List<T> 类与 ArrayList 类比较类似.它实现了 IList<T> 泛型接口,长度可以动态增加. 可以使用 Add 或 AddRange 方法将项添加到 List ...

  9. python程序展现图片

    突然想写一个python程序能够显示图片的 ,展示文字的已经实现了 现在就搞一搞这个吧 相信也是很简单 首先是放一张图片在e盘下面 等会程序打包的时候将会用到 就决定是你啦 皮卡丘: 然后就写代码吧:

  10. 设计基于HTML5的APP登录功能及安全调用接口的方式

    转自:http://blog.csdn.net/linlzk/article/details/45536065 最近发现群内大伙对用Hbuilder做的APP怎么做登录功能以及维护登录状态非常困惑,而 ...