A1133 Splitting A Linked List (25 points | linked list, with detailed annotations, logical analysis)

Keywords: less

Written in front

  • Thinking analysis
    • Understanding the meaning
      • Given a list and K, after traversing the list, the nodes < 0 will be output first, then the nodes in the range of 0-k will be output, and finally the nodes in the range of > k will be output.
    • Specific realization:
      • 1. All nodes are stored in structure {id, data, next}.
      • 2. Traverse the list, find out the nodes in the list and put them in the container v.
      • 3. The nodes are divided into three categories (--infinite, 0), [0,k], (k, + -infinite)], which are put into the capacitive ans in sequence by segment and output at last.
  • There is a certain degree of difficulty in the subject.
    • Understanding the meaning of a question takes time (core question)
    • Achieving Thought is Smart

test case

  • input:
    00100 9 10
    23333 10 27777
    00000 0 99999
    00100 18 12309
    68237 -6 23333
    33218 -4 00000
    48652 -2 -1
    99999 5 68237
    27777 11 48652
    12309 7 33218
    output:
    33218 -4 68237
    68237 -6 48652
    48652 -2 12309
    12309 7 00000
    00000 0 99999
    99999 5 23333
    23333 10 00100
    00100 18 27777
    27777 11 -1
    

ac code

  • #include <iostream>
    #include <vector>
    using namespace std;
    struct node
    {
        int id, data, next;
    };
    int main()
    {
        int bgin, n, k, s, d, e;
        scanf("%d%d%d", &bgin, &n, &k);
        node a[100010];
    
        vector<node> v, ans;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d%d", &s, &d, &e);
            a[s] = {s, d, e};
        }
        // Save the initial list sequence into an array
        for (; bgin != -1; bgin = a[bgin].next)
            v.push_back(a[bgin]);
    
        // Processing less than 0 elements
        for (int i = 0; i < v.size(); i++)
            if (v[i].data < 0) ans.push_back(v[i]);
        // Processing greater than or equal to 0, less than k elements
        for (int i = 0; i < v.size(); i++)
            if (v[i].data >= 0 && v[i].data <= k) ans.push_back(v[i]);
        // Processing elements larger than k
        for (int i = 0; i < v.size(); i++)
            if (v[i].data > k) ans.push_back(v[i]);
    
        for (int i = 0; i < ans.size() - 1; i++)
            printf("%05d %d %05d\n", ans[i].id, ans[i].data, ans[i + 1].id);
    
        printf("%05d %d -1\n", ans[ans.size() - 1].id, ans[ans.size() - 1].data);
        return 0;
    }
    

Summary of Knowledge Points

  • Structural body assignment
    • a[s] = {s, d, e};

Posted by dirkie on Tue, 08 Oct 2019 10:50:24 -0700