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. Mybatis 分页实现

    一.插件 PageHelper(推荐使用) 原理:利用Mybatis的拦截器,截获需要分页的sql语句,在语句后面加分页条件,及获取总记录数等属性. 注意 插件属性类 参考一 参考二 实例: 第一步: ...

  2. 通过usb访问mtp设备(ubuntu12.04) (转载)

    转自:http://robert.penz.name/658/howto-access-mtp-devices-via-usb-on-ubuntu-12-04/ A friend asked me h ...

  3. object-c中实现特定一个或者多个页面横竖屏,其他界面保持竖屏显示。

    1.首先设置项目支持的屏幕方向.info.plist设置(自行设置) 2.写一个子类CusNavigationController 继承 UINavigationController,在CusNavi ...

  4. centos 扩展lrzsz通过xshell 下载安装文件

    yum自动安装: yum install lrzsz 手动安装方法如下: 定制安装的linux可能没有把rzsz包安装到系统,这对用securecrt这样的windows工具传输文件特别不方便.为了使 ...

  5. Manacher HDOJ 3068 最长回文

    题目传送门 关于求解最长回文子串,有dp做法,也有同样n^2的但只用O(1)的空间,还有KMP,后缀数组?? int main(void) { ) == ) { ); memset (dp, fals ...

  6. Android 性能优化(3)性能工具之「调试 GPU 过度绘制」Debug GPU Overdraw Walkthrough-查看哪些view过度绘制了

    Debug GPU Overdraw Walkthrough 1.In this document Prerequisites Visualizing Overdraw You should also ...

  7. ES6 学习笔记 - 变量的解构赋值

    变量的解构赋值 学习资料:ECMAScript 6 入门 数组的解构赋值 基本用法 可以从数组中提取值,按照对应位置,对变量赋值.这种写法属于"模式匹配". let [a, b, ...

  8. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  9. URI URL URN的区别

    一:什么是URI,URL,URN ? URI:Uniform Resource Identifier,统一资源标识符,是一个用于表示互联网上资源名称的字符串 格式:http://www.xxx.com ...

  10. 面试中的一些小问题之ES5和ES6的区别?

    1995年,JavaScript作为网景浏览器的一部分首次发布,起初并不叫JavaScript,而是叫LiveScript,但是因为当时Java正火,也算是为了搭上java的顺风车,于是改成了Java ...