国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合山市| 资中县| 大田县| 芒康县| 天长市| 白水县| 桦南县| 双鸭山市| 德州市| 邮箱| 丹江口市| 囊谦县| 江山市| 南投市| 辉县市| 宁国市| 双城市| 河西区| 彭山县| 绥阳县| 尚志市| 龙门县| 双江| 罗源县| 沈丘县| 阳城县| 大兴区| 根河市| 满洲里市| 肇东市| 新绛县| 广汉市| 廉江市| 罗定市| 东明县| 苏尼特右旗| 新郑市| 凤城市| 阜新市| 石阡县| 界首市|