-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.h
753 lines (685 loc) · 20.6 KB
/
algorithm.h
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#pragma once
#ifndef _ALGORITHM_H
#define _ALGORITHM_H
#include "utility.h"
#include "iterator.h"
#include "functional.h"
namespace ministl
{
template<class BiDirectionalIteartor>
inline void reverse(BiDirectionalIteartor first, BiDirectionalIteartor last)
{
while (true)
{
if (first == last || first == --last)
{
break;
}
else
{
iter_swap(first++, last);
}
}
}
// Non - modifying sequence operations :
template<class ForwardIterator, class Function>
Function for_each(ForwardIterator first, ForwardIterator last, Function f)
{
for (; first != last; first++)
{
f(*first);
}
return f;
}
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& val)
{
typedef typename iterator_traits<ForwardIterator1>::value_type value_type;
find_if(first, last, equal_to<value_type>());
}
template<class InputIterator, class Function>
InputIterator find_if(InputIterator first, InputIterator last, Function f)
{
for (; first != last; first++)
{
if (f(*first))
break;
}
return first;
}
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
if (last1 == first1 || last2 == first2)
return last1;
auto pos = last1;
while (pos != first1)
{
auto pos1 = pos;
auto pos2 = first2;
while (pos1 < last1 && pos2 < last2 && pred(*pos1, *pos2))
{
pos1++, pos2++;
}
if (pos2 == last2)
return pos;
pos--;
}
return last1;
}
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
typedef typename iterator_traits<ForwardIterator1>::value_type value_type;
return ministl::find_end(first1, last1, first2, last2, equal_to<value_type>());
}
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
typedef typename iterator_traits<ForwardIterator1>::value_type value_type;
return ministl::find_first_of(first1, last1, first2, last2, equal_to<value_type>());
}
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
for (; first1 != last; first1++)
{
auto i = first1;
auto j = first2;
while (j != last2 && pred(*i,*j))
i++, j++;
if (j == last2)
break;
}
return first1;
}
template <class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)
{
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
adjacent_find(first, last, equal_to<value_type>());
}
template <class ForwardIterator, class Function>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, Function f)
{
auto p = first++;
while (first != last)
{
if (f(*p, *first))
break;
first++;
p++;
}
if (first == last)
return last;
else
return p;
}
template <class ForwardIterator, class T>
size_t count(ForwardIterator first, ForwardIterator last, const T& value)
{
typename typedef iterator_traits<ForwardIterator>::value_type value_type;
return count_if(first, last, equal_to<value_type>());
}
template <class InputIterator, class Predicate>
size_t count_if(InputIterator first, InputIterator last, Predicate pred)
{
size_t ret = 0;
while (first != last) if (pred(*first++)) ++ret;
return ret;
}
template <class InputIterator1, class InputIterator2>
std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
while (first1 != last1 && (*first1 == *first2))
{
first1++, first2++;
}
return std::pair<InputIterator1, InputIterator2>({ first1,first2 });
}
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred)
{
auto it = first2;
for (; first1 != last1; first1++)
{
if (pred(*first1, *it))
it++;
}
return first1 == last1;
}
template <class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2)
{
typedef typename iterator_traits<InputIterator1>::value_type value_type;
return equal(first1, last1, first2, equal_to<value_type>());
}
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
for (; first1 != last1; first1++)
{
auto i = first1;
auto j = first2;
while (j != last2&&i != last1&& pred(*j,*i))
i++, j++;
if (j == last2)
return first1;
}
return last1;
}
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
typename typedef iterator_traits<ForwardIterator1>::value_type value_type;
return search(first1, last1, first2, last2, equal_to<value_type>());
}
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
Size count, const T& value)
{
for (; first != last; first++)
{
Size cnt = 0;
auto i = first;
while (cnt < count && i != last &&*i == value)
i++, cnt++;
if (cnt == count)
return first;
}
return last;
}
template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
{
for (; first != last; first++)
{
*(result++) = *first;
}
return result;
}
template<class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate func)
{
for (; first != last; first++)
{
if (func(*first))
{
*(result++) = *first;
}
}
return result;
}
template<class InputIterator, class OutputIterator, class Size = size_t>
void copy_n(InputIterator first, Size cnt, OutputIterator result)
{
for (Size i = 0; i < cnt; first++, result++, i++)
*result = *first;
}
template<class InputIterator, class OutputIterator>
OutputIterator copy_backward(InputIterator first, InputIterator last, OutputIterator result)
{
for (; first != last; last--)
{
*(result--) = *last;
}
return result;
}
template<class T>
void swap(T& a, T& b)
{
T t(a);
a = b;
b = t;
}
template < class ForwardIterator1, class ForwardIterator2 >
ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2)
{
for (; first1 != last1; first1++, first2++)
{
swap(*first1, *first2);
}
return first2;
}
template< class ForwardIterator1,class ForwardIterator2 >
void iter_swap(ForwardIterator1 a, ForwardIterator2 b)
{
swap(*a, *b);
}
template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform(InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op)
{
for (; first1 != last1; first1++, result++)
{
*result = op(*first1);
}
return result;
}
template < class InputIterator1, class InputIterator2,class OutputIterator, class BinaryOperator >
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, OutputIterator result,BinaryOperator binary_op)
{
for (; first1 != last1; first1++, first2++, result++)
{
*result = binary_op(*first1, *first2);
}
return result;
}
template < class ForwardIterator, class T >
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
{
for (; first != last; first++)
{
if (*first == old_value) *first = new_value;
}
}
template < class ForwardIterator, class Predicate, class T >
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value)
{
for (; first != last; first++)
{
if (pred(*first)) *first = new_value;
}
}
template < class InputIterator, class OutputIterator, class T >
OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_val, const T& new_val)
{
for (; first != last; first++, result++)
{
*result = *first == old_val ? new_val : *first;
}
return result;
}
template< class InputIterator, class OutputIterator, class Predicate, class T>
OutputIterator reaplace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_val)
{
for (; first != last; first++, result++)
{
*result = pred(*first) ? new_val : *first;
}
return result;
}
template< class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& val)
{
for (; first != last; first++)
{
*first = val;
}
}
template < class OutputIterator, class Size, class T >
void fill_n(OutputIterator first, Size n, const T& value)
{
while (n-- > 0) *(first++) = value;
}
template <class ForwardIterator, class Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen)
{
for (; first != last; first++)
*first = gen();
}
template <class OutputIterator, class Size, class Generator>
void generate_n(OutputIterator first, Size n, Generator gen)
{
while (n-- > 0) *first++ = gen();
}
template<class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& val)
{
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
return remove_if(first, last, equal_to<value_type>());
}
template < class ForwardIterator, class Predicate >
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)
{
ForwardIterator result = first;
for (; first != last; first++)
{
if (!pred(*first))
*result++ = *first;
}
return result;
}
template < class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value)
{
for (; first != last; ++first)
if (!(*first == value)) *result++ = *first;
return result;
}
//并不是所有迭代器都支持随机存取....
/*template< class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last)
{
if (first == last) return first;
ForwardIterator result = first++;
for (; first != last; first++)
{
if (*first != *(first - 1)) *result++ = *first;
}
return result;
}*/
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last,
BinaryPredicate pred)
{
ForwardIterator result = first;
while (++first != last)
{
if (!pred(*first, *result))
{
*(++result) = *first;
}
}
return ++result;
}
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last)
{
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
return unique(first, last, equal_to<value_to>());
}
template<class ForwardIterator>
void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
{
ForwardIterator next = middle;
while (first != next)
{
swap(*first++, *next++);
if (next == last) next = middle;
else if (first == middle) middle = next;
}
}
template<class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
{
while (true)
{
while (first != last && pred(*first)) ++first;
if (first == last--) break;
while (first != last && !pred(*last)) --last;
if (first == last) break;
swap(*first++, *last);
}
return first;
}
//Heap
/**********[Make_heap]:O[N]***********/
template<class RandomAccessIterator, class Comparator>
void up(RandomAccessIterator begin_of_heap, RandomAccessIterator first, RandomAccessIterator last, Comparator comp)
{
if (first == last) return;
size_t index = last - begin_of_heap;
size_t parent_index = (index - 1) / 2;
for (auto cur = last; parent_index >= 0 && cur != begin_of_heap; parent_index = (index - 1) / 2)
{
auto parent = begin_of_heap + parent_index;
if (comp(*parent, *cur))
{
ministl::swap(*parent, *cur);
}
cur = parent;
index = cur - begin_of_heap;
}
}
template<class RandomAccessIterator, class Comparator>
void down(RandomAccessIterator begin_of_heap, RandomAccessIterator first, RandomAccessIterator last, Comparator comp)
{
if (first == last) return;
size_t index = first - begin_of_heap;
size_t child_index = index * 2 + 1;
for (auto cur = first; child_index <= last - begin_of_heap && cur < last; child_index = index * 2 + 1)
{
auto child = begin_of_heap + child_index;
if (child + 1 <= last && comp(*child,*(child + 1)))
{
child = child + 1;
}
if (comp(*cur, *child))
{
ministl::swap(*cur, *child);
}
cur = child;
index = cur - begin_of_heap;
}
}
template<class RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last)
{
ministl::make_heap(first, last, ministl::less<typename iterator_traits<RandomAccessIterator>::value_type>());
}
template<class RandomAccessIterator, class Comparator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Comparator comp)
{
const size_t cnt = last - first;
for (auto cur = first + cnt / 2 - 1; cur >= first; cur--)
{
ministl::down(first, cur, last - 1, comp);
if (cur == first) return;
}
}
/**********[Push_Heap]:O[lgN]***********/
template<class RandomAccessIterator>
void push_heap(RandomAccessIterator first, RandomAccessIterator last)
{
ministl::up(first, first, last - 1, ministl::less<typename iterator_traits<RandomAccessIterator>::value_type>());
}
template<class RandomAccessIterator, class Comparator>
void push_heap(RandomAccessIterator first, RandomAccessIterator last, Comparator cmp)
{
ministl::up(first, first, last - 1, cmp);
}
/**********[Pop_Heap]:O[lgN]***********/
template<class RandomAccessIterator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last)
{
ministl::pop_heap(first, first, last - 1, ministl::less<typename iterator_traits<RandomAccessIterator>::value_type>());
}
template<class RandomAccessIterator, class Comparator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Comparator cmp)
{
swap(*first, *(last - 1));
if (last - first > 1)
ministl::down(first, first, last - 2, cmp);
}
/**********[Sort_Heap]:O[NlgN]***********/
template<class RandomAccessIterator>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last)
{
ministl::sort_heap(first, last, ministl::less<typename iterator_traits<RandomAccessIterator>::value_type>());
}
template<class RandomAccessIterator, class Comparator>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last,Comparator comp)
{
for (auto cur = last; cur != first; cur--)
ministl::pop_heap(first, cur, comp);
}
/**********[Is_Heap]:O[N]***********/
template<class RandomAccessIterator>
bool is_heap(RandomAccessIterator first, RandomAccessIterator last)
{
return ministl::is_heap(first,last, ministl::less<typename iterator_traits<RandomAccessIterator>::value_type>());
}
template<class RandomAccessIterator, class Comparator>
bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Comparator comp)
{
size_t index = (last - first) / 2;
for (auto cur = first + index; cur >= first; cur--, index--)
{
if (*cur < *(first + index * 2 + 1) || (first + index * 2 + 2 < last && *cur < *(first + index * 2 + 2)))
{
return false;
}
}
return true;
}
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last)
{
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
return ministl::sort(first, last, less<value_type>());
}
template<class RandomAccessIterator, class Compare>
RandomAccessIterator comp_partition(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
auto l = first, r = last - 1;
auto val = *first;
while (l < r)
{
while (l < r && !comp(*r, val))
{
r--;
}
if (l < r)
{
*l++ = *r;
}
while (l < r && comp(*l, val))
{
l++;
}
if (l < r)
{
*r-- = *l;
}
}
*l = val;
return l;
}
template <class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
if (first >= last) return;
auto l = comp_partition(first, last, comp);
if (l == first || l == last) return;
ministl::sort(first, l, comp);
ministl::sort(l + 1, last, comp);
}
template <class RandomAccessIterator>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last)
{
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
return ministl::partial_sort(first, middle, last, less<value_type>());
}
template <class RandomAccessIterator, class Compare>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare comp)
{
ministl::make_heap(first, middle, comp);
for (auto pos = middle; pos != last; pos++)
{
if (!comp(*first,*pos))
{
swap(*first, *pos);
ministl::down(first, first, middle - 1, comp);
}
}
ministl::sort(first, middle, comp);
}
template <class RandomAccessIterator, class Compare>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp)
{
if (first >= last) return;
auto val = median(*first, *(last - 1), *(first + (last - first - 1) / 2));
auto pos = comp_partition(first, last, comp);
if (pos < nth)
{
ministl::nth_element(pos + 1, nth, last, comp);
}
else
{
ministl::nth_element(first, nth, pos, comp);
}
}
template <class RandomAccessIterator, class Compare>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last)
{
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
return ministl::nth_element(first, nth, last, less<value_type>());
}
template <class T>
T median(T a, T b, T c)
{
if ((a <= b && a >= c) || (a <= c && a >= b))
{
return a;
}
else if ((b <= a && b >= c) || (b <= c && b >= a))
{
return b;
}
else if ((c <= a && c >= b) || (c <= b && c >= a))
{
return c;
}
}
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp)
{
while (first1 != last1 && first2 != last2)
{
*result++ = (comp(*first1, *first2)) ? *first1++ : *first2++;
}
if (first2 == last2)
{
ministl::copy(first1, last1, result);
}
if (first1 == last1)
{
ministl::copy(first2, last2, result);
}
}
/*
Permutation O(N)
*/
template <class BidirectionalIterator>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last)
{
if (last - first <= 1)
{
return false;
}
auto i = first;
for (; i + 1 != last && *i < *(i + 1); i++)
{}
if (i == last)
{
return false;
}
auto j = i + 1;
for (auto tmp = last - 1; tmp != j + 1; tmp++)
{
if (*tmp > *i && *tmp < *j)
{
j = tmp;
}
}
iter_swap(j, pos);
ministl::reverse(pos + 1, nums.end());
return true;
}
}
#endif