国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

A1052. Linked List Sorting (25)

2019-11-08 03:13:31
字體:
來源:轉載
供稿:網友

1052. Linked List Sorting (25)

時間限制400 ms內存限制65536 kB代碼長度限制16000 B判題程序Standard作者CHEN, Yue

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<cstdio>#include<algorithm>using namespace std;const int maxn = 1e5 + 10;int value[maxn], nt[maxn];  //地址上的值是不變的,下一地址可能會變,用 int op[maxn];               // op數組來組建鏈表 bool cmp(int a, int b){	return value[a] < value[b];}int main(){	int n, root, address, total = 0;	scanf("%d %d", &n, &root);	for(int i = 0; i < n; ++i){		scanf("%d", &address);		scanf("%d %d", &value[address], &nt[address]);	}	for(int p = root; p != -1; p = nt[p]) op[total++] = p;	if(total > 0){		sort(op, op + total, cmp);		printf("%d %05d/n", total, op[0]);		for(int i = 0; i < total; ++i){			if(i != total - 1){				printf("%05d %d %05d/n", op[i], value[op[i]], op[i + 1]);			}else{				printf("%05d %d -1/n", op[i], value[op[i]]);			}		}	}else{		printf("0 -1/n");    //沒有數據時,特判為1	}	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通城县| 三明市| 宝应县| 奉新县| 大渡口区| 东乌珠穆沁旗| 乌拉特前旗| 凤山县| 新津县| 德兴市| 昭觉县| 武山县| 武隆县| 祁东县| 安徽省| 凉山| 枣庄市| 山阳县| 富民县| 买车| 连江县| 沂源县| 济源市| 广灵县| 永州市| 宝应县| 阳新县| 长沙市| 酒泉市| 赤峰市| 抚顺市| 乡宁县| 马边| 乌拉特后旗| 沾化县| 邢台市| 抚松县| 东山县| 万宁市| 东乌珠穆沁旗| 娄烦县|