国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莫力| 扎兰屯市| 沾益县| 陈巴尔虎旗| 达孜县| 南溪县| 凤台县| 武川县| 石柱| 康保县| 垦利县| 广州市| 仁布县| 大渡口区| 土默特右旗| 乾安县| 邳州市| 罗城| 桃江县| 无锡市| 綦江县| 康保县| 松滋市| 遵义市| 维西| 乐陵市| 溆浦县| 拉萨市| 伊春市| 沾化县| 马鞍山市| 龙州县| 义乌市| 鹰潭市| 凤台县| 黑水县| 聂荣县| 泾阳县| 库车县| 南靖县| 南丰县|