PAT甲题题解-1052. Linked List Sorting (25)-排序

三个注意点:

1.给出的n个节点并不一定都在链表中

2.最后一组样例首地址即为-1

3.输出地址的时候一直忘记前面要补0。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn=100000+5;
struct Node{
    int addr;
    int val;
    int to;
    bool operator<(const Node tmp)const{
        return val<tmp.val;
    }
}linked[maxn],node[maxn];
int main()
{
    int n,first;
    int a,b,c;
    scanf("%d %d",&n,&first);
    for(int i=0;i<n;i++){
        scanf("%d %d %d",&a,&b,&c);
        linked[a].addr=a;
        linked[a].val=b;
        linked[a].to=c;
    }
    int cnt=0;
    while(first!=-1){
        node[cnt].addr=linked[first].addr;
        node[cnt].val=linked[first].val;
        node[cnt].to=linked[first].to;
        cnt++;
        first=linked[first].to;
    }
    sort(node,node+cnt);
    if(cnt==0){
        printf("0 -1\n");  //最后一个样例有首地址为-1的情况。。。
        return 0;
    }
    printf("%d %05d\n",cnt,node[0].addr);
    for(int i=0;i<cnt;i++){
        printf("%05d %d ",node[i].addr,node[i].val);
        if(i==cnt-1)
            printf("-1\n");
        else
            printf("%05d\n",node[i+1].addr);
    }
    return 0;
}

时间: 2024-08-08 13:44:18

PAT甲题题解-1052. Linked List Sorting (25)-排序的相关文章

PAT甲题题解-1012. The Best Rank (25)-排序水题

排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询,建立id与索引的映射即可. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <map> using namespace s

PAT甲题题解-1036. Boys vs Girls (25)-找最大最小,大水题

题意:给出n个人的姓名.性别.ID.分数,让你找出其中哪个妹纸分数最高.哪个汉子分数最低.以及他们的差如果没有妹纸或者汉子,则对应输出Absent,差用NA代替. 就是for一遍找最大最小值,水题 #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> #include <vector> #defin

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

PAT甲题题解-1007. Maximum Subsequence Sum (25)-求最大子区间和

题意:给出n个数,求最大连续的子区间和,并且输出该区间的第一个和最后一个数. 如果所有数都小于0,那么则输出0,第一个数和最后一个数. 看数据k的范围,就知道肯定不能两层for循环来求区间和,O(n^2)的复杂度肯定超时所以这里肯定要求一遍for循环就能知道结果定义区间l和r,sum为目前[l,r]之间的和一开始l=r=0,sum=a[0]接下来,对于当前第i个数字a[i]如果sum+a[i]>a[i],那么就将a[i]一起加入到区间中去.如果sum+a[i]<a[i],那么还不如不加,直接重

PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘

多项式相乘 注意相乘结果的多项式要开两倍的大小!!! #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> using namespace std; //两个多项式相乘 const int maxn=1000+5; int k; //原本定义个exp导致编译错误,没想到定义成exp1.exp2也会编译错误,

PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里用的是dfs,dfs的复杂度只要O((m+n)*k)这里k是指因为有k个点要查询,每个都要求一下删除后的联通分支数.题目没给定m的范围,所以如果m很大的话,dfs时间会比较小. for一遍1~n个点,每次从一个未标记的点u开始dfs,标记该dfs中访问过的点.u未标记过,说明之前dfs的时候没访问过

PAT甲题题解-1017. Queueing at Bank (25)-模拟

有n个客户和k个窗口,给出n个客户的到达时间和需要的时长有空闲的窗口就去办理,没有的话就需要等待,求客户的平均时长.如果在8点前来的,就需要等到8点.如果17点以后来的,则不会被服务,无需考虑. 按客户的到达时间排序建立一个优先级队列,一开始放入k个窗口,初始结束时间为8*3600然后for循环客户,每次从优先级队列中取出最早结束时间的窗口如果客户比结束时间来的早,就需要等待如果客户比结束时间来的晚,就无需等待最后只要统计那些到达时间在17*3600之前的客户即可. #include <iost

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