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. [转]Android长度单位详解

    android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...

  2. AngularJS进阶(二十二)实现时间选择插件

    JS实现时间选择插件 引导语 在项目开发过程中,需要实现根据以日期为筛选条件之一,故需要实现时间选择插件.对于未接触的新事物,自己总是感觉不明觉厉.其实,有些实现可以使用很简单的方法即可.以此为例,偶 ...

  3. Github Coding Developer Book For LiuGuiLinAndroid

    Github Coding Developer Book For LiuGuiLinAndroid 收集了这么多开源的PDF,也许会帮到一些人,现在里面的书籍还不是很多,我也在一点点的上传,才上传不到 ...

  4. Leetcode_53_Maximum Subarray

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43989997 Find the contiguous su ...

  5. Leetcode_137_Single Number II

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42877129 Given an array of inte ...

  6. Dynamics CRM 请求服务时报access is denied错误

    在javascript脚本里使用ODATA向组织服务发请求时报错,请求被拒绝了,后经人点拨得知是脚本跨域的问题,原因如下:访问系统我用的是IP地址,但通过 Xrm.Page.context. cont ...

  7. 关于Service中bindService注意的几个问题

    最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...

  8. 【58】Spring总结之注解(2)

    启用注解配置: 1.context:annotation-config 该配置表示启用注解. 2.context:component-scan base-package="." 该 ...

  9. MySql常用操作语句(2:数据库、表管理以及数据操作)

    本文主要内容转自一博文. 另外可供参考资源: SQL语句教程 SQL语法 1.数据库(database)管理  1.1 create 创建数据库 mysql> create database f ...

  10. iOS多线程篇:NSThread简单介绍和使用

    一.什么是NSThread NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程, 需要手动管理线程的生命周期,处理线程同 ...