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. 一个小公式帮你轻松将IP地址从10进制转到2进制

    网络工程师经常会遇到的一个职业问题:如何分配IP,通过子网如何捕捉某一网段或某台机器?他们甚至能够进行精准的分析和复杂的开发......凡此种种,其实与一些他们头脑中根深蒂固的常识性理论存有某种内在的 ...

  2. SpriteBuilder中子节点的相对位置(%百分比定位)

    子节点(或在这里确切的为精灵sprites)50%的偏移效果使得其在父节点中居中显示,该父节点的纹理在左下角(锚点为0,0). 这样做好过用父节点的位置的实际值来定位.根据父节点实际位置来定位在早期的 ...

  3. 数据库事务的4个特性ACID

    原子性(Atomicity[ætə'mɪsɪti])原型atomic.一致性(Consistency).隔离性(Isolation).持久性(Durability)

  4. 如何设计一个可用的web容器

    之前在另外一个平台(http://www.jointforce.com/jfperiodical/article/1035)发表的一篇文章,现在发布到自己的博客上. 开发一个web容器涉及很多不同方面 ...

  5. Linux - grep的一些进阶选项

    [root@www ~]# grep [-A] [-B] [--color=auto] '搜寻字串' filename 选项与参数: -A :后面可加数字,为 after 的意思,除了列出该行外,后续 ...

  6. obj-c编程10:Foundation库中类的使用(5)[时间对象]

    隔了好久才有了这新的一篇,还是无奈的时间啊!so这次我们就着重谈谈它喽. F库中有很多时间相关的类,比如NSDate,NSTimeInterval,NSTimeZone,NSDateComponent ...

  7. sxoi爆炸祭

    好吧,纯粹是去玩玩的,我这么一个弱省的蒟蒻,进队纯粹是开玩笑.... Day0 去五中试机,感觉电脑手感不错,打了半个线段树的板子才发现试机要在自己的电脑上试,然后我无奈的搬东西(从26号搬到2号), ...

  8. 第15章-输入/输出 --- File类

    (一) Java的IO通过java.io包下的类和接口来支持,在java.io包下主要包括输入.输出两种IO流. 每种输入.输出流又分为字节流和字符流两大类: (1)字节流以字节为单位来处理输入.输出 ...

  9. Fiddler抓包使用教程-断点调试

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/62896784 本文出自[赵彦军的博客] Fiddler 里面的断点调试有2种方式. ...

  10. ThinkPHP简单的验证码实现

    ThinkPHP简单的验证码实现 写一个最简单的TP验证码. 写Controller 首先在Controller/IndexController.class.php(简称Index)文件中编辑: &l ...