1052 Linked List Sorting 25

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;
struct Node{
    int address,data,next,flag;
}nodes[maxn];
bool cmp(Node a, Node b){
    if(a.flag != b.flag) return a.flag > b.flag;
    else return a.data < b.data;
}
int main() {
    int n,head;
    scanf("%d%d", &n, &head);
    int tmp;
    for(int i = 0; i < n; i++){
        scanf("%d", &tmp);
        scanf("%d%d", &nodes[tmp].data, &nodes[tmp].next);
        nodes[tmp].address = tmp;
        nodes[tmp].flag = 0;
    }
    int valid = 0;
    int cur;
    for(cur = head; cur != -1; cur=nodes[cur].next){
        valid++;
        nodes[cur].flag = 1;
    }
    if(valid == 0){
        printf("0 -1\n");
        return 0;
    }
    sort(nodes, nodes+maxn, cmp);
    printf("%d %05d\n", valid, nodes[0].address);
    for(int i = 0; i < valid; i++){
        if(i != valid - 1){
            printf("%05d %d %05d\n", nodes[i].address, nodes[i].data, nodes[i+1].address);
        }else{
            printf("%05d %d -1\n", nodes[i].address, nodes[i].data);
        }
    }
    return 0;
}
相关推荐
老鱼说AI30 分钟前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda
罗湖老棍子1 小时前
【例 1】数列操作(信息学奥赛一本通- P1535)
数据结构·算法·树状数组·单点修改 区间查询
big_rabbit05021 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode
张李浩2 小时前
Leetcode 15三题之和
算法·leetcode·职场和发展
2301_793804693 小时前
C++中的适配器模式变体
开发语言·c++·算法
x_xbx3 小时前
LeetCode:206. 反转链表
算法·leetcode·链表
abant23 小时前
leetcode 138 复制随机链表
算法·leetcode·链表
ab1515173 小时前
3.17二刷基础112 118 完成进阶52
数据结构·算法
美式请加冰3 小时前
链表的介绍和使用
数据结构·链表
旖-旎3 小时前
二分查找(1)
c++·算法·二分查找·力扣·双指针