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 (≤103). 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
#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 分)——链表的更多相关文章
- 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 v ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 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 ...
- PAT乙级:1090危险品装箱(25分)
PAT乙级:1090危险品装箱(25分) 题干 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清 ...
- PAT乙级:1070 结绳 (25分)
PAT乙级:1070 结绳 (25分) 题干 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟 ...
- 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 ( ...
- 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 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
随机推荐
- 利用反射调用注解,模仿Spring
简介 在开发中,我们经常用的就是利用@RequestMapping来调用我们自己的逻辑,现在我们来创建属于自己的注解模仿一下它. 1.新建属于自己的注解@SeayaMapping @Target({E ...
- ArrayList 原码解析
基于java1.71.ArrayList 是基于数组实现的 通过源码 我们可以知道 ArrayList 提供了三种构造器用于实例化 /** * Constructs an empty list wit ...
- 20个Chrome DevTools调试技巧
译者按: Chrome DevTools很强大,甚至可以替代IDE了! 原文: Art of debugging with Chrome DevTools 译者: Fundebug 为了保证可读性,本 ...
- MAC下搭建个人博客
安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/inst ...
- canvas-6font.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BUGList
Django : a. MySQL数据表还未创建时,不可在视图内直接使用模型类对象,产生报错 django.db.utils.ProgrammingError: (1146, "Table ...
- 微信小程序request同步请求
今天在搞微信小程序的时候顺手用了async,await死活不起作用,后来查了一下子,竟然不支持,那没办法就换了一种实现wx.request同步请求的方案 祭出promise来搞一搞,下面直接贴代码,简 ...
- sqlserver每行都有累加,你怎么搞,窗口函数来帮忙!!!
在统计中我们经常会面临复杂的查询,例如累加,最近也遇到一个很好的例子,今天我们一起来看看: 有一个关于销售数据的视图,字段有:emptyid(员工编号),ordermonth( ...
- Windows 10修复
[以管理员运行如下命令]: 1.sfc /scannow 命令将扫描所有受保护的系统文件,并用位于 %WinDir%\System32\dllcache 的压缩文件夹中的缓存副本替换损坏的文件. 2. ...
- python自动化开发-9 进程 线程
进程与线程 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一 ...