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 ...
随机推荐
- Workspace in use or cannot be created, choose a different one.--错误解决的方法
eclipse 使用一段时间后.有时会由于一些故障自己就莫名奇异的关闭了,再打开时有时没有问题,有时有会提示错误 Workspace Unavailable: Workspace in use or ...
- 扩展VirtualBox中的centos硬盘大小
一.克隆文件 我之前安装的时候建的是centos 6.3.可是后来空间不够,没办法,又不想重装centos.由于好多东西要配置,特麻烦,所以先想到了使用resize命令,可是在win8中运行D:\Pr ...
- Python3基础(四) 条件与循环控制
Python的流程控制语句包括:if条件语句.while循环语句.for循环语句.range函数以及break.continue.pass控制语句.这些语句在Python中的语义和在其他语言中基本是一 ...
- Java课程设计——人事管理系统
主界面代码: package PersonSystem; import java.awt.*; import java.awt.event.*; import javax.swing.*; impor ...
- Flash--元件和实例
1.元件简述: 元件在Flash影片中是一种特殊的对象.在Flash中仅仅须要创建一次,然后能够在整部电影中重复使用而不会显著添加 文件大小. 事实上在使用元件时,我们一般使用的是该元件的实例,所以说 ...
- Linux 下Python调用C++编写的动态库
在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...
- 【Codeforces】 Round #374 (Div. 2)
Position:http://codeforces.com/contest/721 我的情况 开始还是rank1,秒出C.(11:00机房都走光了,我ma到11:05才走,只打了一个小时) 结果.. ...
- MSP430:中断简介
(5).中断应用程序举例(外部中断): void interrupt_initial() { P1DIR&=~BIT7; //P1.7为输入 P1IE|=0x80; //P ...
- 同一个Tomcat下不同项目之间的session共享
最近发现项目运行过程中经常会抛出一个 NullPointerException的异常,经检查发现异常出现的地方是日志模板,一阵检查,正常无误 (把所有记录日志的地方都点了一遍,心里是崩溃的),万念俱灰 ...
- codevs3981动态最大子段和(线段树)
3981 动态最大子段和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 题目还是简单一点好... 有n个数,a ...