Source:

PAT A1133 Splitting A Linked List (25 分)

Description:

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 (≤) which is the total number of nodes, and a positive K (≤). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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 [, 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

Keys:

  • 链表
  • 散列(Hash)

Code:

 /*
Data: 2019-08-09 14:34:04
Problem: PAT_A1133#Splitting A Linked List
AC: 23:34 题目大意:
重排单链表,不改变原先顺序的前提下,使得
1.负数在前,正数在后;
2.正数中,小于K元素在前,大于K的在后;
输入:
第一行给出,首元素地址,结点数N<=1e5,阈值K
接下来N行,地址,数值,下一个地址
输出:
地址,数值,下一个地址 基本思路:
结构体存储链表信息
按顺序存储链表中有效的数值,再按要求排序,输出;
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=1e5+;
struct node
{
int adr,data,nxt;
}link[M],t;
vector<node> p1,p2,p; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int fst,n,k;
scanf("%d%d%d", &fst,&n,&k);
for(int i=; i<n; i++)
{
scanf("%d%d%d", &t.adr,&t.data,&t.nxt);
link[t.adr] = t;
}
while(fst != -)
{
if(link[fst].data < )
p.push_back(link[fst]);
else if(link[fst].data <=k)
p1.push_back(link[fst]);
else
p2.push_back(link[fst]);
fst = link[fst].nxt;
}
p.insert(p.end(),p1.begin(),p1.end());
p.insert(p.end(),p2.begin(),p2.end()); for(int i=; i<p.size(); i++)
{
if(i!=) printf("%05d\n", p[i].adr);
printf("%05d %d ", p[i].adr, p[i].data);
}
printf("-1"); return ;
}

PAT_A1133#Splitting A Linked List的更多相关文章

  1. PAT1133:Splitting A Linked List

    1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

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

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

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

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

  4. A1133. Splitting A Linked List

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

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

  6. 1133 Splitting A Linked List

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

  7. PAT 1133 Splitting A Linked List

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

  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 A1133 Splitting A Linked List (25) [链表]

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

随机推荐

  1. 【解决】hive与hbase表结合级联查询的问题

    [Author]: kwu [解决]hive与hbase表结合级联查询的问题.hive两个表以上,关联查询时出现长时无法返回的情况. 同一时候也不出现,mr的进度百分比. 查询日志如图所看到的: 解决 ...

  2. 解决华为手机不出现logcat日志的问题

    问题描写叙述:公司一部华为手机在连接Eclipse时在Logcat中看不到相关日志 解决方法:1 进入手机拨号界面2 输入*#*#2846579#*#*3 输入完成后自己主动跳转到測试界面4 依次选择 ...

  3. 可设置指定时间自己主动消失的 MessageBox实现

    本文主要是讲怎样实现可设置指定时间自己主动消失的 MessageBox提示框ShowMessageBoxTimeout实现: 在开发client应用程序的时候.经经常使用得WinForm中Messag ...

  4. # 导入模块 from wxpy import * # 初始化机器人,扫码登陆 bot = Bot()

    # 导入模块 from wxpy import * # 初始化机器人,扫码登陆 bot = Bot()

  5. XAML实例教程系列 - 对象和属性(二)

    XAML实例教程系列 - 对象和属性 2012-05-22 14:18 by jv9, 1778 阅读, 6 评论, 收藏, 编辑 在前一篇已经介绍XAML概念:“XAML语言是Extensible ...

  6. 树的遍历 迭代算法——思路:初始化stack,pop stack利用pop的node,push new node to stack,可以考虑迭代一颗树 因为后序遍历最后还要要访问根结点一次,所以要访问根结点两次是难点

    144. Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' ...

  7. c#用webkit内核支持html5

    [实例简介]经过测试可用 [实例截图] [核心代码] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 using System; ...

  8. Codeforces Round #239 (Div. 1)

    B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. P4407 [JSOI2009]电子字典

    传送门 我的哈希打挂了--然而大佬似乎用哈希可以过还跑得很快-- 删除,枚举删哪个字符,记删之后的哈希值存map 插入,相当于在单词里删字符,去对应的map里查找 更改,相当于两个都删掉同一个位置的字 ...

  10. [Swift通天遁地]二、表格表单-(10)快速添加日期选择/多选/动作表单/地图等自定义表单

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...