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;
}
相关推荐
PXM的算法星球1 小时前
【leetcode】3524 求出数组的X值1
算法·leetcode·职场和发展
椰羊~王小美3 小时前
LeetCode -- Flora -- edit 2025-04-27
算法·leetcode·职场和发展
缘友一世4 小时前
从线性回归到逻辑回归
算法·逻辑回归·线性回归
前端_学习之路5 小时前
javaScript--数据结构和算法
javascript·数据结构·算法
weixin_428498495 小时前
使用HYPRE库并行装配IJ稀疏矩阵指南: 矩阵预分配和重复利用
算法·矩阵
雾削木7 小时前
mAh 与 Wh:电量单位的深度解析
开发语言·c++·单片机·嵌入式硬件·算法·电脑
__lost7 小时前
小球在摆线上下落的物理过程MATLAB代码
开发语言·算法·matlab
8RTHT8 小时前
数据结构(七)---链式栈
数据结构
mit6.8249 小时前
[Lc_week] 447 | 155 | Q1 | hash | pair {}调用
算法·leetcode·哈希算法·散列表
Fency咖啡10 小时前
《代码整洁之道》第9章 单元测试 - 笔记
数据结构·b树