zoukankan      html  css  js  c++  java
  • A1052. Linked List Sorting

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.

    Then N lines follow, each describes a node in the format:

    Address Key Next

    where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

    Output Specification:

    For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

    Sample Input:

    5 00001
    11111 100 -1
    00001 0 22222
    33333 100000 11111
    12345 -1 33333
    22222 1000 12345
    

    Sample Output:

    5 12345
    12345 -1 00001
    00001 0 11111
    11111 100 22222
    22222 1000 33333
    33333 100000 -1

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 typedef struct NODE{
     5     int lt, rt;
     6     int data;
     7     int valid;
     8     NODE(){
     9         valid = 0;
    10     }
    11 }node;
    12 node nds[100001];
    13 bool cmp(node a, node b){
    14     if(a.valid != b.valid){
    15         return a.valid > b.valid;
    16     }else return a.data < b.data;
    17 }
    18 using namespace std;
    19 int main(){
    20     int N, head, addr;
    21     scanf("%d%d", &N, &head);
    22     for(int i = 0; i < N; i++){
    23         scanf("%d", &addr);
    24         nds[addr].lt = addr;
    25         scanf("%d %d", &nds[addr].data, &nds[addr].rt);
    26     }
    27     int pt = head, cnt = 0;
    28     while(pt != -1){
    29         nds[pt].valid = 1;
    30         cnt++;
    31         pt = nds[pt].rt;
    32     }
    33     sort(nds, nds + 100001, cmp);
    34     if(cnt > 0)
    35         printf("%d %05d
    ", cnt, nds[0].lt);
    36     else printf("0 -1");
    37     for(int i = 0; i < cnt; i++){
    38         if(i == cnt - 1){
    39             printf("%05d %d -1
    ", nds[i].lt, nds[i].data);
    40         }else{
    41             printf("%05d %d %05d
    ", nds[i].lt, nds[i].data, nds[i + 1].lt);
    42         }
    43     }
    44     cin >> N;
    45     return 0;
    46 }
    View Code

    总结:

    1、题意:将所给的节点重新排序。 但注意,仅仅是合法节点进行排序。合法节点是指按照第一行所给的首地址能串在一起的所有节点。题目会给孤立的非法节点,需要注意区分。办法就是对合法节点标记1,非法节点标记0。然后利用排序进行分类,以及二级排序。

    2、当没有合法节点时,要注意输出特殊情况 0 -1。

    3、在读入每一个节点的时候,不要忘记给节点的地址赋值。

  • 相关阅读:
    接口的显示实现和隐式实现
    Math.Round和四舍五入
    经典SQL语句大全(转)
    简明添加log4net到项目中
    NAnt学习笔记(3) Properties, Loggers & Listeners
    (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
    Pyramid中如何配置多种URL匹配同一个View
    《IT项目管理》读书笔记(4) —— 项目范围管理
    C#语法糖
    枚举类型转换成字符串
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8531310.html
Copyright © 2011-2022 走看看