PAT_A1133#Splitting A Linked List
Source:
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 [, andNext
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的更多相关文章
- PAT1133:Splitting A Linked List
1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- A1133. Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- 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 ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- PAT 1133 Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- 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 ...
- 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 ...
随机推荐
- HDU 4499 Cannon (暴力搜索)
题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每一个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include & ...
- python基础练习-猜年龄、编写登陆接口小程序
python基础练习: 一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...
- 查看及更改MySQL数据库物理文件存放的位置
查看命令: mysql> show global variables like "%datadir%"; 表格第二行即为文件的位置.另外,可以在该文件夹的配置文件my.i ...
- swift 2.0 语法 数组
import UIKit /*: 数组 * 格式 var arr:[Int] = [数值1, 数值2, 数值3] * 不可变数组 let == NSArray * 可变数组 var */ l ...
- 5.3.3 deque对象
class collections.deque([iterable[, maxlen]]) 返回一个新双向队列,当有输入迭代器时.会从左至右地加入到队列里.假设没有输入參数,就创建一个空队列. deq ...
- MapReduce01
================== Hadoop内核 | MapReduce(分布式计算框架) ================== 源于Google的MapReduce论文 ----------& ...
- NOI.AC #31. MST
好像又是神仙dp....gan了一早上 首先这是个计数类问题,上DP, 对于一个最小生成树,按照kruskal是一个个联通块,枚举边小到大合成的 假如当前边是树边,那么转移应该还是枚举两个块然后合并 ...
- bzoj2841
边双联通分量 具体详解蓝书上十分详细,因为必须是奇数个人坐在一起,那么一个人如果能选上,就必须处在一个简单奇圈中.而奇圈也是一个边双联通分量,所以我们先把边双联通分量都挖出来,然后进行二分图染色. 奇 ...
- 特征变化--->特征向量中部分特征到类别索引的转换(VectorIndexer)
VectorIndexer: 倘若所有特征都已经被组织在一个向量中,又想对其中某些单个分量进行处理时,Spark ML提供了VectorIndexer类来解决向量数据集中的类别性特征转换. 通过为其提 ...
- MyBatis Generator实现MySQL分页插件
MyBatis Generator是一个非常方便的代码生成工具,它能够根据表结构生成CRUD代码,可以满足大部分需求.但是唯一让人不爽的是,生成的代码中的数据库查询没有分页功能.本文介绍如何让MyBa ...