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;
}
相关推荐
未到结局,焉知生死5 分钟前
PAT每日三题11-20
c++·算法
Aaron158829 分钟前
通用的通感控算存一体化平台设计方案
linux·人工智能·算法·fpga开发·硬件工程·射频工程·基带工程
就是ping不通的蛋黄派39 分钟前
数据结构与算法—线性表(C++描述)
数据结构·c++
_w_z_j_44 分钟前
拼三角(枚举)
算法
hweiyu001 小时前
数据结构和算法分类
数据结构·算法·分类
M K Q1 小时前
2025.9 GESP三级 日历制作
算法
AI小云1 小时前
【数据操作与可视化】Pandas数据处理-Series数据结构
开发语言·数据结构·python·numpy·pandas
x***J3481 小时前
算法竞赛训练方法
算法
前端小L1 小时前
图论专题(十六):“依赖”的死结——用拓扑排序攻克「课程表」
数据结构·算法·深度优先·图论·宽度优先
前端小L1 小时前
图论专题(十三):“边界”的救赎——逆向思维解救「被围绕的区域」
数据结构·算法·深度优先·图论