【PAT甲级】1052 Linked List Sorting (25 分)

题意:

输入一个正整数N(<=100000),和一个链表的头结点地址。接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址。地址由五位包含前导零的正整数组成。以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址。

trick:

题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗。。。

数据点1包含有些结点并非在头结点所在的这条链表上。(题干说给一条联通的链表,实际可能有些点并不在该链表上)

通过一些题目可见,有些trick不会明显的在题干中给出,题干只保证一些一定不会发生的情况,还有一些边界条件以及可能出现的问题需要自行多加考虑。。。。。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
pair<int,int>pr[100007];
pair<int,int>ans[100007];
int main(){
int n;
int add;
scanf("%d%d",&n,&add);
for(int i=1;i<=n;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
pr[x]={y,z};
}
int cnt=0;
while(1){
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+1,ans+1+cnt);
printf("%d %05d\n",cnt,ans[1].second);
for(int i=1;i<=cnt;++i){
printf("%05d %d ",ans[i].second,ans[i].first);
if(i!=cnt)
printf("%05d\n",ans[i+1].second);
else
printf("-1");
}
}
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11619280.html

时间: 2024-08-27 22:35:20

【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 necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

1052 Linked List Sorting (25分)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the stru

PAT Advanced 1052 Linked List Sorting (25) [链表]

题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the s

PAT:1052. Linked List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int address; int data; int next; bool tag; }Node[100066]; bool cmp(node a,node b) { if(a.tag!=b.tag) return a.tag>b.tag; else return a.data<

PAT 1052. Linked List Sorting (25)

1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

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

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

PAT 甲级 1083 List Grades (25 分)

1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. I

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth