题目

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

  1. 用数组存放结点,用属性order记录结点在链表中的序号(初始化为3*maxn)
  2. 依次遍历链表中结点

    2.1 如果结点值<0,结点序号设置为cnt1++

    2.2 如果结点值>=0&&<=k,结点序号设置为maxn+cnt2++

    2.3 如果结点值>k,结点序号设置为2*maxn+cnt3++
  3. 按照order排序,并依次打印

思路 02

  1. 用数组存放结点,用属性order记录结点在链表中的序号(初始化为3*maxn)
  2. 依次遍历链表中结点

    2.1 依次遍历链表中结点,找到所有值<0的节点,存放于vector

    2.2 依次遍历链表中结点,找到所有值>=0&&<=k的节点,存放于vector

    2.3 依次遍历链表中结点,找到所有值>k的节点,存放于vector
  3. 依次打印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) [链表]的更多相关文章

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

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

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

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

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

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

  7. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

  8. A1133. Splitting A Linked List

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

  9. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

随机推荐

  1. Java IO流学习总结(转)

    原文地址:http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 ...

  2. you-get加ffmpeg获取视频素材并转格式

    最近做视频,觉得素材不好下载,下载了转格式又很麻烦,终于,在网上ob了很久的我找到了属于自己的工具. you-get视频下载 当你在网上找视频素材的时候发现了一个自己觉得很有意思的视频,但是获取这个视 ...

  3. 51nod 1006:最长公共子序列Lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  4. jsp页面引入不了js路径没错

    最近搞开发,发现有个jsp页面引入不了js:很是神奇,路径什么的都没问题,同事的浏览器可以加载该js,发现放到其他的文件夹下可以加载该js:当时没研究出来,任务紧就没研究了. 最近闲下来了,有去研究, ...

  5. git push 现有代码到一个新的分支

    git push origin HEAD:task/xxx-test-local git push的一般形式为 git push <远程主机名> <本地分支名>  <远程 ...

  6. python 简单字符串字典加密

    1 def crypt(source,key): from itertools import cycle result='' temp=cycle(key) for ch in source: res ...

  7. JS-语句五

    for循环的实例 1.九九乘法表: 1*1  1*2  1*3        1*2  2*2  2*3        1*3  2*3  3*3        1*4  2*4  4*3       ...

  8. CentOS 7.3 安装redis 4.0.2服务

    CentOS 7.3 安装redis 4.0.2服务 1.下载解压 下载地址:/home/xiaoming/ wget http://download.redis.io/releases/redis- ...

  9. ES6 之 对象的扩展

    1.Object.is() 判断俩个值是否相等 +0 不等于 -0 NaN 等于自身 console.log(Object.is('foo','foo')); // true console.log( ...

  10. 程序员必备:详解XSS和CSRF

    做开发的小伙伴想必都不陌生XSS 和 CSRF,但也有一些刚接触的朋友还不是很清楚,今天就给大家详解下XSS和CSRF! 一.XSS xss,即 Cross Site Script,中翻译是跨站脚本攻 ...