PAT A1133 Splitting A Linked List (25) [链表]
题目
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
题目分析
已知N个结点,将值为负数的结点放置在非负节点之前,将小于等于k的结点放置在大于k的结点之前
解题思路
思路 01
- 用数组存放结点,用属性order记录结点在链表中的序号(初始化为3*maxn)
- 依次遍历链表中结点
2.1 如果结点值<0,结点序号设置为cnt1++
2.2 如果结点值>=0&&<=k,结点序号设置为maxn+cnt2++
2.3 如果结点值>k,结点序号设置为2*maxn+cnt3++ - 按照order排序,并依次打印
思路 02
- 用数组存放结点,用属性order记录结点在链表中的序号(初始化为3*maxn)
- 依次遍历链表中结点
2.1 依次遍历链表中结点,找到所有值<0的节点,存放于vector
2.2 依次遍历链表中结点,找到所有值>=0&&<=k的节点,存放于vector
2.3 依次遍历链表中结点,找到所有值>k的节点,存放于vector - 依次打印vector中结点
易错点
已知结点中存在无效结点(即不在链表中的结点)
Code
Code 01(最优)
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
int order=3*maxn;
} nds[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char * argv[]) {
int hadr,n,k,adr;
scanf("%d %d %d",&hadr,&n,&k);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int cnt1=0,cnt2=0,cnt3=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
if(nds[i].data<0)nds[i].order=cnt1++;
if(nds[i].data>=0&&nds[i].data<=k)nds[i].order=maxn+cnt2++;
if(nds[i].data>k)nds[i].order=2*maxn+cnt3++;
}
sort(nds,nds+maxn,cmp);
int cnt=cnt1+cnt2+cnt3;
for(int i=0; i<cnt-1; i++) {
printf("%05d %d %05d\n",nds[i].adr,nds[i].data,nds[i+1].adr);
}
printf("%05d %d -1\n",nds[cnt-1].adr,nds[cnt-1].data);
return 0;
}
Code 02
#include <iostream>
#include <vector>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
} nds[maxn];
int main(int argc,char * argv[]) {
int hadr,n,k,adr;
scanf("%d %d %d",&hadr,&n,&k);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
vector<node> v,ans;
for(int i=hadr; i!=-1; i=nds[i].next) {
v.push_back(nds[i]);
}
for(int i=0; i<v.size(); i++) {
if(v[i].data<0)ans.push_back(v[i]);
}
for(int i=0; i<v.size(); i++) {
if(v[i].data>=0&&v[i].data<=k)ans.push_back(v[i]);
}
for(int i=0; i<v.size(); i++) {
if(v[i].data>k)ans.push_back(v[i]);
}
for(int i=0; i<ans.size()-1; i++) {
printf("%05d %d %05d\n",ans[i].adr,ans[i].data,ans[i+1].adr);
}
printf("%05d %d -1\n",ans[ans.size()-1].adr,ans[ans.size()-1].data);
return 0;
}
PAT A1133 Splitting A Linked List (25) [链表]的更多相关文章
- 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 Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
- 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 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- 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 Advanced 1097 Deduplication on a Linked List (25) [链表]
题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplica ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- A1133. Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT甲题题解-1032. Sharing (25)-链表水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
随机推荐
- 十、JavaScript之文本相加
一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 数据抽象
数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节. 数据抽象是一种依赖于接口和实现分离的编程(设计)技术. 它们向外界提供了大量用于操作对象数据的公共方法,也 ...
- Android数据库(sqlite)之Room
说在前面: 1.使用Room需要添加的依赖: dependencies { def room_version = "2.2.3" implementation "andr ...
- Python Learning Day8
bp4解析库 pip3 install beautifulsoup4 # 安装bs4pip3 install lxml # 下载lxml解析器 html_doc = """ ...
- Java生鲜电商平台-如何使用微服务来架构生鲜电商B2B2C平台?
Java生鲜电商平台-如何使用微服务来架构生鲜电商B2B2C平台? 说明:随着互联网的日益普及,人们通过手机下单买菜的人越来越多,生鲜这个行业有两个显著的特点,一个是刚需.(你每天都要吃饭,都要吃菜) ...
- [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome
Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...
- 掌握这三点,轻松搞定Essay写作
英文essay写作涉及的范围很广,任何文字形式的材料都涉及写作.所以,不单单是专业的文字工作者要在写作上下功夫,一般人在从小到大的学校教育里要应对的作文.读书报告.美国高中及大学里的论文.英文演讲以及 ...
- tortoiseGit 的简单使用说明
拉取仓库到本地 参考 下面几张图片,把仓库拉取到本地. 本地修改并推送 进入文件夹后,按照 下面几张图片切换到本地的开发分支 当修改完成之后,按照 下面几张图片 的方法把修改推送到远程仓库的开发分支. ...
- HZNU-ACM寒假集训Day8小结 最小生成树
最小生成树(无向图) Kruskal 给所有边按从小到大排序 形成环则不选择(利用并查集) P1546 最短网络 https://www.luogu.com.cn/problem/P1546 #i ...
- 通过Android的API对Sqlite数据库进行操作
一.增删改查 增 改 查 删 这是删除之前 删除三条 Dao.java package com.example.databasedemo; import android.content.Content ...