File tree 2 files changed +36
-2
lines changed
2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 1
1
#ifndef CP_ALGO_UTIL_COMPRESS_COORDS_HPP
2
2
#define CP_ALGO_UTIL_COMPRESS_COORDS_HPP
3
- #include < algorithm >
3
+ #include " sort.hpp "
4
4
#include < vector>
5
5
namespace cp_algo {
6
6
// coords is a range of reference_wrapper<T>
7
7
auto compress_coords (auto &coords) {
8
8
std::vector<int > original;
9
9
original.reserve (size (coords));
10
- std::ranges::sort (coords);
10
+ radix_sort (coords);
11
11
int idx = -1 , prev = -1 ;
12
12
for (auto &x: coords) {
13
13
if (x != prev) {
Original file line number Diff line number Diff line change
1
+ #ifndef CP_ALGO_UTIL_SORT_HPP
2
+ #define CP_ALGO_UTIL_SORT_HPP
3
+ #include < algorithm>
4
+ #include < numeric>
5
+ #include < ranges>
6
+ #include < vector>
7
+ namespace cp_algo {
8
+ void count_sort (auto &a, size_t maxc, auto &&proj = std::identity{}) {
9
+ std::vector<int > cnt (maxc);
10
+ for (auto &x: a) {
11
+ cnt[proj (x)]++;
12
+ }
13
+ std::partial_sum (begin (cnt), end (cnt), begin (cnt));
14
+ auto res = a;
15
+ for (auto const & it: a | std::views::reverse) {
16
+ res[--cnt[proj (it)]] = it;
17
+ }
18
+ a = std::move (res);
19
+ }
20
+
21
+ void radix_sort (auto &a) {
22
+ if (empty (a)) {
23
+ return ;
24
+ }
25
+ int base = std::bit_floor (size (a));
26
+ auto mx = std::ranges::max (a);
27
+ for (int64_t i = 1 ; i <= mx; i *= base) {
28
+ count_sort (a, base, [&](auto x) {
29
+ return x / i % base;
30
+ });
31
+ }
32
+ }
33
+ }
34
+ #endif // CP_ALGO_UTIL_SORT_HPP
You can’t perform that action at this time.
0 commit comments