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

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

A1074. Reversing Linked List (25)

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

1074. Reversing Linked List (25)

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

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.

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 (<=N) which is the length of the sublist to be reversed. 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, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:
00100 6 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
可以直接轉置地址
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 1e5 + 10;int value[maxn], nt[maxn];  //存放值的數組   下一地址數組 int op[maxn];               //操作數組 int main(){	int head, n, k, address;	scanf("%d %d %d", &head , &n, &k);	for(int i = 0; i < n; ++i){		scanf("%d", &address);		scanf("%d %d", &value[address], &nt[address]);	}	int total = 0;	for(int i = head; i != -1; i = nt[i]){		op[total++] = i;	}	for(int i = 0; i + k <= total; i +=k){		for(int low = i, high = low + k - 1; low < high; ++low, --high){			swap(op[low], op[high]);		}	}	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]]);		}	}	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀安县| 县级市| 寿阳县| 万年县| 壤塘县| 达州市| 安康市| 木里| 哈巴河县| 柳河县| 雅安市| 双江| 和林格尔县| 保定市| 富民县| 溧水县| 永年县| 舞阳县| 武威市| 绥棱县| 桑植县| 巴里| 丰顺县| 晴隆县| 永昌县| 平昌县| 会昌县| 浮梁县| 嘉兴市| 阳泉市| 霍城县| 淅川县| 洛南县| 若羌县| 建昌县| 杨浦区| 如东县| 来安县| 新河县| 兖州市| 海林市|