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;
}
相关推荐
田里的水稻1 天前
FA_建图和定位(ML)-超宽带(UWB)定位
人工智能·算法·数学建模·机器人·自动驾驶
Navigator_Z1 天前
LeetCode //C - 964. Least Operators to Express Number
c语言·算法·leetcode
郝学胜-神的一滴1 天前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
摸鱼仙人~1 天前
算法题避坑指南:数组/循环范围的 `+1` 到底什么时候加?
算法
liliangcsdn1 天前
基于似然比的显著图可解释性方法的探索
人工智能·算法·机器学习
骇城迷影1 天前
代码随想录:二叉树篇(中)
数据结构·c++·算法·leetcode
Zhu_S W1 天前
深入理解哈希表:原理、源码与设计哲学
数据结构·散列表
期末考复习中,蓝桥杯都没时间学了1 天前
力扣刷题23
算法·leetcode·职场和发展
菜鸡儿齐1 天前
leetcode-子集
算法·leetcode·深度优先
今儿敲了吗1 天前
28| A-B数对
数据结构·c++·笔记·学习·算法