PAT甲级练习1052. Linked List Sorting (25)
来源:互联网 发布:政府网络舆论互动交流 编辑:程序博客网 时间:2024/06/06 00:54
1052. Linked List Sorting (25)
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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <set>#include <stack>#include <string>#include <string.h>using namespace std;const int MAX=1e5+10;struct Node{int address;int key;int next;bool flag;}node[MAX];bool cmp(Node a, Node b){if(a.flag==false || b.flag==false)return a.flag > b.flag;elsereturn a.key < b.key;}int main(){int n, start, cnt=0, a, b, c;scanf("%d%d", &n, &start);for(int i=1; i<=n; i++){scanf("%d%d%d", &a, &b, &c);node[a].address = a;node[a].key = b;node[a].next = c;}for(int i=start; i != -1; i=node[i].next){node[i].flag = true;cnt++;}if(cnt==0) printf("0 -1\n");else{sort(node, node+100000, cmp);printf("%d %05d\n", cnt, node[0].address);for(int i=0; i<cnt; i++){printf("%05d %d", node[i].address, node[i].key);if(i!=cnt-1)printf(" %05d\n", node[i + 1].address);elseprintf(" -1\n");}}cin>>n;return 0;}
- PAT甲级练习1052. Linked List Sorting (25)
- 【PAT甲级】1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25) PAT甲级
- PAT甲级1052. Linked List Sorting (25)
- PAT 甲级 1052. Linked List Sorting (25)
- PAT甲级 1052. Linked List Sorting (25)
- PAT 甲级 1041 Linked List Sorting (25)
- 1052. Linked List Sorting (25)-PAT甲级真题
- PAT甲级练习1028. List Sorting (25)
- PAT甲级真题及训练集(17)--1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)-PAT
- 【PAT】1052. Linked List Sorting (25)
- PAT (Advanced) 1052. Linked List Sorting (25)
- PAT A 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT(A) - 1052. Linked List Sorting (25)
- java.lang.ClassNotFoundException: net.sf.navigator.menu.MenuContextListener错误
- mysqldump备份数据
- Android NDK 实例
- 1083. List Grades
- 递归 求阶乘
- PAT甲级练习1052. Linked List Sorting (25)
- 使用 URLDecoder 和 URLEncoder 对中文字符进行编码和解码
- 邹晓艺专栏:Deep Learning系列笔记(一)
- JavaScript的原型和原型链
- React Native通讯机制
- yslow的安装(注意兼容性)
- IO流以及多线程的学习
- 看职场老人教你如何平稳度过职场“更年期”
- react-native nativeBase小结