-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1521.cpp
235 lines (199 loc) · 6.42 KB
/
1521.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
source:https://acm.timus.ru/problem.aspx?num=1521
First of all: Я ненавижу эту задачу
Пояснения к примененному алгоритму:
Из временного ограничения следует, что данную задачу надо решать меньше, чем за n^2, из-за чего не подойдет очевидный вариант с хранением значений в списке и их последовательным удалением.
Будем хранить все значения в полном бинарном дереве. На нижнем уровне будут располагаться 1 или 0 в соответствии с тем, остался ли в строю солдат с номером, соответствующим его порядковому номеру среди листьев этого дерева. В узле i будем хранить сумму его левого и правого поддерева.
Подобные подход позволпфаходить следующего солдата, который должен выбыть, за NlogN.
*/
/// this problem is pure evil, have fun
#include <iostream>
#include <cstdint>
enum direction{
NONE,
ROOT,
TOP,
LEFT,
RIGHT
};
template <class T, class Index>
class Tree
{
T * values;
Index last_level;
Index size;
Index pop_step;
Index current_node;
void remove(Index cur_node)
{
do
values[cur_node]--;
while(to_father(cur_node));
}
bool to_left_son(Index& cur_node)
{
if (2 * cur_node + 2 < size)
{
cur_node = 2 * cur_node + 1;
return true;
}
else return false;
}
bool to_right_son(Index& cur_node)
{
if (2 * cur_node + 2 < size)
{
cur_node = 2 * cur_node + 2;
return true;
}
else return false;
}
bool to_father(Index& cur_node)
{
if (cur_node != 0)
{
cur_node = (cur_node - 1) / 2;
return true;
}
else return false;
}
public:
Tree (Index N, Index K)
{
last_level = 1;
while (last_level < N)
last_level = last_level << 1u;
size = (last_level << 1u) - 1;
pop_step = K;
values = new T[size];
current_node = 0;
for (Index i = 0; i < N; i++)
values[i + last_level - 1] = 1;
for (Index i = N; i < last_level; i++)
values[i + last_level - 1] = 0;
for (Index temp, i = last_level - 2; i > 0; i--)
{
values[i] = 0;
if (to_left_son(temp = i))
values[i] += values[temp];
if (to_right_son(temp = i))
values[i] += values[temp];
}
}
T pop()
{
if (!current_node)
{
current_node = last_level - 2 + pop_step;
return current_node - last_level + 2;
}
remove(current_node);
Index counter = pop_step;
direction dir = NONE;
Index father;
while(counter > 0)
{
Index temp_left = current_node, temp_right = current_node;
to_left_son(temp_left);
to_right_son(temp_right);
switch(dir)
{
case NONE:
{
father = current_node;
to_father(father);
to_right_son(father);
dir = father == current_node ? RIGHT : LEFT;
current_node = father;
break;
}
case ROOT:
{
counter -= values[current_node];
if (counter > 0)
{
dir = LEFT;
to_father(current_node);
}
break;
}
case TOP:
{
if(current_node > last_level - 1)
counter--;
else if(values[temp_left] < counter )
{
counter -= values[temp_left];
dir = TOP;
current_node = temp_right;
}
else
{
dir = TOP;
current_node = temp_left;
}
break;
}
case LEFT:
{
if (values[temp_right] < counter)
{
counter -= values[temp_right];
if (current_node == 0)
{
current_node = last_level - 1;
dir = ROOT;
}
else
{
to_father(father = current_node);
to_right_son(father);
dir = father == current_node ? RIGHT : LEFT;
to_father(current_node);
}
}
else
{
dir = TOP;
current_node = temp_right;
}
break;
}
case RIGHT:
{
if (current_node == 0)
{
current_node = last_level - 1 ;
dir = ROOT;
}
else
{
father = current_node;
to_father(father);
to_right_son(father);
dir = father == current_node ? RIGHT : LEFT;
to_father(current_node);
}
break;
}
}
}
return current_node - last_level + 2;
}
};
using namespace std;
int main()
{
int n, k;
cin >> n >> k;
if (k == 1)
{
for (uint32_t i = 1; i <= n; i++)
cout << i << " ";
return 0;
}
Tree<uint32_t, uint32_t> tree = Tree<uint32_t, uint32_t>(n, k);
for (int i = 0; i < n; i++)
cout << tree.pop() << " ";
return 0;
}