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;
}
相关推荐
~|Bernard|27 分钟前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师30 分钟前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo332 分钟前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
好家伙VCC2 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
liulilittle3 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx5 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐6 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸6 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
tju新生代魔迷7 小时前
数据结构:双向链表
数据结构·链表
一只懒洋洋7 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类