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. Android百度地图SDK 导航初始化和地图初始化引起的冲突

    如题,相同是百度地图SDK开发过程中遇到的一个问题.交代下背景: 开发了一款内嵌百度地图的应用,因此里面差点儿相同将眼下百度地图SDK开放的主要功能都用到了,定位,地图显示,覆盖物标示.POI搜索,行 ...

  2. map, string 强大的STL

    hdu 1247  Hat's Words Input Standard input consists of a number of lowercase words, one per line, in ...

  3. HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求。

    HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求. 2018-04-20 14:00 by 码农小周, 21 阅读, 2 评论, 收藏, 编辑 ...

  4. Android应用资源

    Java刚開始学习的人直接在Java源代码使用"hello" 和123 类型的字符串和整型.但时间长了就会忘记当初定义的原因,有经验的或许会定义字符串常量ResultSet.TYP ...

  5. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

  6. Scala 返回多个值

    class A{ var c var d def return={ (c,d,"soyo") //以元组形式返回 }}调用: val s=new A var(a1,a2,a3)=s ...

  7. tinymce 富文本编辑器 编写资料

    tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...

  8. Java的安装过程

    记录一下自己在Windowns下安装java的过程 安装网址:http://www.oracle.com/index.html 打开网址后要先登录,如果没有号就先注册,然后才能下载 step1:下载J ...

  9. Factstone Benchmark(数学)

    http://poj.org/problem?id=2661 题意:Amtel在1960年发行了4位计算机,并实行每十年位数翻一番的策略,将最大整数n作为改变的等级,其中n!表示计算机的无符号整数(n ...

  10. P3379最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...