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 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;
class node
{
public:
int val;
int next;
};
vector<node> nodes(100000);
int main()
{
vector<vector<int>> res(3);
int start,N,K;
while(cin >> start >> N >> K)
{
for(int i = 0;i < N;i++)
{
int tmp;
cin >> tmp;
cin >> nodes[tmp].val >> nodes[tmp].next;
}
while(start != -1)
{
if(nodes[start].val < 0)
res[0].push_back(start);
else if(nodes[start].val >= 0 && nodes[start].val <= K)
res[1].push_back(start);
else
res[2].push_back(start);
start = nodes[start].next;
}
int cnt = 0;
for(int i = 0;i < res.size();i++)
{
for(int j = 0;j < res[i].size();j++)
{
if(cnt++ == 0)
{
printf("%05d %d ",res[i][j],nodes[res[i][j]].val);
}
else
{
printf("%05d\n%05d %d ",res[i][j],res[i][j],nodes[res[i][j]].val);
}
}
}
printf("-1");
}
}

  

PAT1133:Splitting A Linked List的更多相关文章

  1. PAT-1133 Splitting A Linked List(链表分解)

    Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...

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

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

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

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

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

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

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

  7. 1133 Splitting A Linked List

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

  8. PAT 1133 Splitting A Linked List

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

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

随机推荐

  1. Linux Platform Device and Driver

    从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...

  2. BMP 转 YUV (BMP2YUV)

    本文介绍BMP 转 YUV.其实这是以前"数据压缩"实验课上的内容,前几天有人问我相关的问题,突然发现自己有一段时间没有接触BMP也有些生疏了,因此翻出资料总结一下. BMP文件格 ...

  3. c语言部分库函数,代码实现,以及细节理解

    代码来自: http://blog.csdn.net/v_JULY_v //得9 分 //为了实现链式操作,将目的地址返回,加2 分! char * strcpy( char *strDest, co ...

  4. Dll的编写 在unity中加载

    1. 在VS中新建Dll项目 2.在头文件中对函数进行声明 extern "C" int _declspec(dllexport) testunity(); 3.在源文件中写函数体 ...

  5. LeetCode之旅(18)-Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  6. <<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步(1)

    <<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步 并发问题是所有问题的基础,也是操作系统设计的基础.并发包括很多设计问题,其中有进程间通信,资源共享与竞争,多个 ...

  7. 锋利的Jquery摘要

    一本好书值得去反复回味 第一章: jquery中的$(document).ready(function(){})与js中的windows.onload()的比较   $(document).ready ...

  8. JAVA调用数据库存储过程

    下面将举出JAVA对ORACLE数据库存储过程的调用          ConnUtils连接工具类:用来获取连接.释放资源 复制代码 package com.ljq.test; import jav ...

  9. Web 前台优化

    大型网站--前端性能优化和规范 2013-10-28 09:00 by 贤达, 2769 阅读, 10 评论, 收藏, 编辑   Web性能涉及的范围太广,但一般web开发者在程序上线以后很多都曾遇到 ...

  10. Day5_递归_二分法

    递归调用: 在调用一个函数的过程中,直接或间接的调用函数本身. def func(): print('from func') 间接调用: def foo(): print('form foo') ba ...