模拟题,注意当k == 1 与 k == n时情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 |
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <map> using
namespace std; const
int N = 100005; struct
Node { int
pre; int
value; int
lat; }node[N]; int
order[N]; int
size; map< int , int > pre2idx; void
solve1( int
fir, int
n) { map< int , int >::iterator it = pre2idx.find(fir); order[size++] = it->second; while
(node[it->second].lat != -1) { it = pre2idx.find(node[it->second].lat); order[size++] = it->second; } } void
solve2( int
k, int
n) { int
bound = n / k * k; int
idx = k; while
(idx <= bound) { int
boundt = idx; int
boundb = idx - k + 1; while
(boundt >= boundb) { int
next = 0; if
(boundt > boundb) next = boundt - 1; else { if
(idx < bound) next = idx + k; else
if (idx == bound) { if
(bound < n) next = idx + 1; else
if (bound == n) { printf( "%05d %d -1\n" , node[order[boundt]].pre, node[order[boundt]].value); break ; } } } printf( "%05d %d %05d\n" , node[order[boundt]].pre, node[order[boundt]].value, node[order[next]].pre); boundt--; } idx += k; } bound++; while
(bound <= n) { if
(node[order[bound]].lat != -1) printf( "%05d %d %05d\n" , node[order[bound]].pre, node[order[bound]].value, node[order[bound]].lat); else
printf( "%05d %d -1\n" , node[order[bound]].pre, node[order[bound]].value); bound++; } } int
main() { int
fir, n, k; while
(scanf( "%d%d%d" , &fir, &n, &k) != EOF) { pre2idx.clear(); size = 1; for
( int
i = 0; i < n; i++) { scanf( "%d%d%d" , &node[i].pre, &node[i].value, &node[i].lat); pre2idx.insert(make_pair(node[i].pre, i)); } solve1(fir, n); solve2(k , size - 1); } return
0; } /* 00100 6 6 00000 4 99999 00100 1 -1 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218 */ |
1074. Reversing Linked List (25),布布扣,bubuko.com
1074. Reversing Linked List (25)
原文:http://www.cnblogs.com/echobfy/p/3576798.html