zoukankan      html  css  js  c++  java
  • PAT 甲级 1052 Linked List Sorting

    https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464

    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 (<) 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 −.

    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 [−], 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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1e5 + 10;
    int N, St;
    
    struct Node {
        int st;
        int key;
        int en;
    }node[maxn], t;
    
    vector<Node> num;
    
    bool cmp(const Node& a, const Node& b) {
        return a.key < b.key;
    }
    
    int main() {
        scanf("%d%d", &N, &St);
        for(int i = 1; i <= N; i ++) {
            scanf("%d%d%d", &t.st, &t.key, &t.en);
            node[t.st].st = t.st;
            node[t.st].key = t.key;
            node[t.st].en = t.en;
        }
    
        while(St != -1) {
            num.push_back(node[St]);
            St = node[St].en;
        }
    
        if(num.size() == 0) {
            printf("0 -1
    ");
            return 0;
        }
    
        sort(num.begin(), num.end(), cmp);
    
        printf("%d %05d
    ", num.size(), num[0].st);
        for(int i = 0; i <num.size() - 1; i ++) {
            printf("%05d %d %05d
    ", num[i].st, num[i].key, num[i + 1].st);
        }
        printf("%05d %d -1
    ", num[num.size() - 1].st, num[num.size() - 1].key);
        return 0;
    }
    

      

  • 相关阅读:
    一般处理程序页ashx 序列化 Json数组
    SQL server 分页
    MySQL 分页
    获取网站的BaseURL
    java学习书籍推荐
    查询并关闭指定端口进程
    ettercap使用
    MS10-046漏洞利用
    MS12-020漏洞利用
    入侵安卓手机
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9852900.html
Copyright © 2011-2022 走看看