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 (≤10​5​​) which is the total number of nodes, and a positive K (≤10​3​​). 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 [−10​5​​,10​5​​], 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

 #include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=;
struct node{
int data;
int pos=;
int address;
int next;
int rank;
}nodes[maxn];
vector<node> v;
int n,k,root;
bool cmp(node n1,node n2){
if(n1.rank<n2.rank) return true;
else if(n1.rank>n2.rank) return false;
else{
return n1.pos<n2.pos;
}
}
int main(){
scanf("%d %d %d",&root,&n,&k);
for(int i=;i<n;i++){
int first,data,next;
scanf("%d %d %d",&first,&data,&next);
node tmp;
tmp.address = first;
tmp.data = data;
tmp.next = next;
if(data<) tmp.rank=;
else if(data<=k) tmp.rank=;
else tmp.rank=;
nodes[first]=tmp;
//v.push_back(tmp);
}
int j=;
while(root!=-){
nodes[root].pos=j;
v.push_back(nodes[root]);
root=nodes[root].next;
j++;
}
sort(v.begin(),v.end(),cmp);
for(int i=;i<v.size();i++){
if(i!=v.size()-){
printf("%05d %d %05d\n",v[i].address,v[i].data,v[i+].address);
}
else {
printf("%05d %d -1\n",v[i].address,v[i].data);
}
}
}

注意点:看到只想着排序了,看大佬的方法真简单,直接三个vector,再合起来打印输出。

一开始最后第二个测试点,最后一个超时,超时是因为一开始存数据直接存在vector里,然后要得到正确的顺序每次都要遍历一遍整个vector,时间复杂度很高。而且最开始的pos其实设置的也是有问题的,直接根据输入顺序得到了,神奇的是结果居然前几个都是对的。

最后存储链表还是要用静态数组,不能用vector,找next太耗时

PAT A1133 Splitting A Linked List (25 分)——链表的更多相关文章

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

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

  3. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

  4. PTA 02-线性结构3 Reversing Linked List (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...

  5. PAT乙级:1090危险品装箱(25分)

    PAT乙级:1090危险品装箱(25分) 题干 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清 ...

  6. PAT乙级:1070 结绳 (25分)

    PAT乙级:1070 结绳 (25分) 题干 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟 ...

  7. PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)

    7-2 Block Reversing (25分)   Given a singly linked list L. Let us consider every K nodes as a block ( ...

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

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

  9. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

随机推荐

  1. Tomcat报failed to parse the expression [${xxx}]异常(javax.el.ELException)的解决方法

    Tomcat 7 'javax.el.ELException' 的解决方式tomcat 7对EL表达式的语法要求比较严格,例如"${owner.new}"因包含关键字new就会导致 ...

  2. HDU6124

    Euler theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)T ...

  3. Python全栈学习_day009知识点

    今日大纲: . 函数的初识 . 函数的返回值 . 函数的参数 1. 函数的初识 统计字符串s的总个数(不能用len) s='fkahfkahofijalkfkadhfkjadhf' count = f ...

  4. git命令详解( 四 )

    此篇为git命令详解的第四篇,话不多说,我们直接上知识点好吧 git Push 偏离的工作 gitPush: 此命令负责将你的变更上传到指定的远程仓库,并在远程仓库上合并你的新提交记录.一旦 git ...

  5. React中使用百度地图API

    今天我们来搞一搞如何在React中使用百度地图API好吧,最近忙的头皮发麻,感觉身体被掏空,所以很久都没来写博客了,但今天我一定要来一篇好吧 话不多说,我们直接开始好吧 特别注意:该React项目是用 ...

  6. [VUE ERROR] Invalid options in vue.config.js: "publicPath" is not allowed

    项目在build的时候报的这个错误: 具体原因是因为版本支持的问题,publicPath 属性到 vue-cli 3.2.0 之后才支持,所以将 publicPaht 改成 baseUrl 即可,或者 ...

  7. 西安OpenParty11月29日活动高清图文回顾——新增西安APEC蓝美图!

    本次活动由西安OpenParty负责线下活动组织运营,线上由InfoQ-QClub.OSChina协办. OSChina活动召集帖:运维为王——应用系统.DevOps与Docker(11月29日) I ...

  8. JMeter 逻辑控制之While循环控制器(While Controller)

    逻辑控制之While循环控制器(While Controller)   by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13 1.   添加While Control ...

  9. Jenkins 利用Dashboard View插件管理任务视图

    利用Dashboard View插件管理任务视图   by:授客 QQ:1033553122 步骤 1.  安装Dashboard View插件 说明: 如果无法在线安装,可以选择本地上传方式安装 附 ...

  10. Java:匿名类,匿名内部类

    本文内容: 内部类 匿名类 首发日期 :2018-03-25 内部类: 在一个类中定义另一个类,这样定义的类称为内部类.[包含内部类的类可以称为内部类的外部类] 如果想要通过一个类来使用另一个类,可以 ...