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. 关于Yuri Boykov and Vladimir Kolmogorov 于2004年提出的max flow / min cut的算法的详解

    出处:http://blog.csdn.net/euler1983/article/details/5959622 算法优化algorithmgraphtree任务 这篇文章说的是Yuri Boyko ...

  2. 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)

    使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...

  3. 一键安装 redmine on windows 和发邮件设置

    一键安装 redmine on windows 和发邮件设置 1)使用http://bitnami.org/stack/redmine一键安装redmine (windows). 2)修改下面的文件: ...

  4. Media Player Classic - HC 源代码分析 1:整体结构

    ===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...

  5. How tomcat works 读书笔记十七 启动tomcat 下

    在上一节中,我们程序的起始位置还是Bootstrap,现在我们通过bat文件来启动这个类. 在分析catalina.bat之前,我们先看看几个简单的我们能用到的dos命令. 基础知识 1 rem 注释 ...

  6. 【45】java的封装剖析

    类是构造对象的模板或蓝图. 封装的一些概念 从形式上看,封装不过是将数据和行为组合到一个包中,并对对象的使用者隐藏了数据的实现形式. 每个对象都包含实例域和方法.实例域的集合代表了一个集合的状态,通过 ...

  7. C语言之linux内核实现位数高低位互换

    linux内核实在是博大精深,有很多优秀的算法,我之前在工作中就遇到过位数高低位交换的问题,那时候对于C语言还不是很熟练,想了很久才写出来.最近在看内核的时候看到有内核的工程师实现了这样的算法,和我之 ...

  8. IIS部署asp.net mvc网站

    iis配置简单的ASP.NET MVC网站编译器:VS 2013本地IIS:IIS 7操作系统:win 7MVC版本:ASP.NET MVC4sql server版本: 2008 r2 打开VS 20 ...

  9. Cloud Carousel

    <div class="carousel1" id="carousel1" > <a href="#"><im ...

  10. 关于web页面JApplet打印小票

    版权所有 做这个的例子太少,我把我做的示例亮出来 一.先说说需要的版本 1.我用的浏览器只有ie: 火狐只支持52版本以下,并且是java7.java8.chrome不支持 2.applet客户端打印 ...