【PAT甲级】1052 Linked List Sorting (25 分)
题意:
输入一个正整数N(<=100000),和一个链表的头结点地址。接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址。地址由五位包含前导零的正整数组成。以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址。
trick:
题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗。。。
数据点1包含有些结点并非在头结点所在的这条链表上。(题干说给一条联通的链表,实际可能有些点并不在该链表上)
通过一些题目可见,有些trick不会明显的在题干中给出,题干只保证一些一定不会发生的情况,还有一些边界条件以及可能出现的问题需要自行多加考虑。。。。。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
pair<int,int>pr[];
pair<int,int>ans[];
int main(){
int n;
int add;
scanf("%d%d",&n,&add);
for(int i=;i<=n;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
pr[x]={y,z};
}
int cnt=;
while(){
if(pr[add].second){
ans[++cnt].first=pr[add].first;
ans[cnt].second=add;
add=pr[add].second;
}
else
break;
}
if(!cnt)
printf("0 -1");
else{
sort(ans+,ans++cnt);
printf("%d %05d\n",cnt,ans[].second);
for(int i=;i<=cnt;++i){
printf("%05d %d ",ans[i].second,ans[i].first);
if(i!=cnt)
printf("%05d\n",ans[i+].second);
else
printf("-1");
}
}
return ;
}
【PAT甲级】1052 Linked List Sorting (25 分)的更多相关文章
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- 1052 Linked List Sorting (25分)
题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
随机推荐
- [AGC027E]ABBreviate
Description AGC027E 给定一个仅由\(AB\)构成的字符串\(S\),给定两个操作,把\(AA\)换成\(B\),和把\(BB\)换成\(A\),问由这个字符串和任意次操作可以得到几 ...
- jenkins新建任务详解
一.新建任务 点击左侧新建任务 输入任务名称-->选择构建一个自由风格的软件项目-->确认 进入任务配置页面 二.基本配置 General 配置构建保留天数,保留个数,必要时并发构建 源码 ...
- try catch 语句中有return 的各类情况
在牛客上做java题时遇到过多到关于try catch语句的问题,看了很多答案解析,在这里记录一下. 首先给出一道题目: 下面代码的运行结果为? A.catch语句块 和是43 B.编译异常 C.fi ...
- flutter web 配置环境及运行(windows)
此下 操作 都是基于 windows 一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...
- Docker on startup: “No activity detected on VM, aborting”
windows下安装的docker,切换到linux,一直处于重启中,最后报No activity detected on VM, aborting错误 上网百度一下这个错误,基本上没人遇到过,最后在 ...
- 第十七篇 Linux下常用命令汇总
- Java中查询某个日期下所有时间段的数据
除了利用时间段进行查询外,还有一个方法: 利用mybatis中的函数,将datetime转为date <if test="purch_date!= null and purch_dat ...
- c++中sort函数调用报错Expression : invalid operator <的内部原理
当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a, const int &b) { if(a!=b) return a<b; ...
- 数据提取之JSON与JsonPATH
数据提取之JSON与JsonPATH JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.适 ...
- dbGet net trace instant pin
proc rn { net_name } {puts " "puts "Net name : $net_name : "set name_rule [dbget ...