Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=1000). The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1. Then N lines follow, each describes a node in the format: Address Data Next where Address is the position of the node, Data is an integer in [-105, 105], and Next is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
voidspit(node *nodes, int first, int k){ while (first != -1) { if (nodes[first].value < 0) { neg.push_back(nodes[first]); } elseif (nodes[first].value <= k) { lesser.push_back(nodes[first]); } else { greater.push_back(nodes[first]); } first = nodes[first].next; } }
vector<node> ans;
voidmerge(){ ans = neg; for (int i = 0; i < lesser.size(); i++) { ans.push_back(lesser[i]); } for (int i = 0; i < greater.size(); i++) { ans.push_back(greater[i]); } }
voidprintAns(){ int i = 0; for (; i < ans.size() - 1; i++) { printf("%05d %d %05d\n", ans[i].first, ans[i].value, ans[i + 1].first); } printf("%05d %d -1\n", ans[i].first, ans[i].value); }
intmain(){ int first = 0, n = 0, k = 0, temp = 0; scanf("%d %d %d", &first, &n, &k); node nodes[100000]; for (int i = 0; i < n; i++) { scanf("%d", &temp); scanf("%d %d", &nodes[temp].value, &nodes[temp].next); nodes[temp].first = temp; } spit(nodes, first, k); merge(); printAns();
Given a sequence of positive integers and another positive integer p. The sequence is said to be a “perfect sequence” if M <= m * p where M and m are the maximum and minimum numbers in the sequence, respectively. Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (<= 105) is the number of integers in the sequence, and p (<= 109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.