- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
class Solution {
public:
std::vector<std::vector<int>> diagonalSort(std::vector<std::vector<int>> & mat) {
if (!mat.size()) return mat;
const size_t rl = mat[0].size();
const size_t cl = mat.size();
sort(mat, rl, cl, 0, 0);
for (size_t i = 1; i < rl; ++i) {
sort(mat, rl, cl, 0, i);
}
for (size_t i = 1; i < cl; ++i) {
sort(mat, rl, cl, i, 0);
}
return mat;
}
private:
void sort(std::vector<std::vector<int>> & mat, size_t rl, size_t cl, size_t i, size_t j) {
const size_t len = std::min(rl - j, cl - i);
const size_t endj = j + len;
const size_t endi = i + len;
std::sort(diag_iter<false>{&mat, i, j}, diag_iter<false>{&mat, endi, endj});
}
template <bool isConst>
class diag_iter {
std::vector<std::vector<int>> *base;
size_t i, j;
using T = int;
public:
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = typename std::conditional<isConst, const T&, T&>::type;
diag_iter(std::vector<std::vector<int>> *base, size_t i, size_t j) : base(base), i(i), j(j) { }
diag_iter(const diag_iter&) = default;
diag_iter& operator=(const diag_iter&) = default;
~diag_iter() = default;
reference operator*() const { return (*base)[i][j]; }
diag_iter& operator++() { i++; j++; return *this; }
friend bool operator== (const diag_iter& a, const diag_iter& b) { return a.i == b.i && a.j == b.j; };
friend bool operator!= (const diag_iter& a, const diag_iter& b) { return !(a == b); };
pointer operator->() const { return &(this->operator*()); }
diag_iter operator++(int) { diag_iter tmp = *this; ++(*this); return tmp; }
diag_iter() = default;
diag_iter& operator--() { i--; j--; return *this; }
diag_iter operator--(int) { diag_iter tmp = *this; --(*this); return tmp; }
diag_iter& operator+=(difference_type n) { i += n; j += n; return *this; }
friend diag_iter operator+(diag_iter it, difference_type n) { return it += n; }
diag_iter& operator-=(difference_type n) { i -= n; j -= n; return *this; }
diag_iter operator-(difference_type n) const { return diag_iter(*this) -= n; }
friend difference_type operator-(const diag_iter& a, const diag_iter& b) { return (b.j * b.base->size() + b.i) - (a.j * a.base->size() + a.i); }
reference operator[](difference_type n) const { return *(*this + n); }
friend bool operator<(const diag_iter& a, const diag_iter& b) { return b - a > 0; }
friend bool operator>(const diag_iter& a, const diag_iter& b) { return b < a; }
friend bool operator>=(const diag_iter& a, const diag_iter& b) { return !(a < b); }
friend bool operator<=(const diag_iter& a, const diag_iter& b) { return !(a > b); }
};
};
ASD_77 26.07.2021 11:17 # 0
Ragulinho 26.07.2021 12:43 # 0
ASD_77 26.07.2021 16:41 # +1
JloJle4Ka 26.07.2021 17:12 # 0
ASD_77 26.07.2021 17:44 # +1
JloJle4Ka 26.07.2021 17:47 # 0
Я не знал что так можно писать.
ASD_77 26.07.2021 18:15 # 0
grillow1337 26.07.2021 18:34 # 0
Soul_re@ver 26.07.2021 13:02 # +2
grillow1337 26.07.2021 18:30 # +1
У себя компилировал с разной оптимизацией (gcc 10.2.1):
Результаты для матрицы 10 000 x 1000:
iter-O0 5606 ms
iter-O3 360 ms
vec-O0 267 ms
vec-O3 45 ms
ASD_77 27.07.2021 03:21 # 0
reference operator*() const { return (*base)[i][j]; }
а вот это что за багор?
using reference = typename std::conditional<isConst, const T&, T&>::type;
grillow1337 27.07.2021 16:46 # 0
https://en.cppreference.com/w/cpp/named_req/InputIterator
*i returns reference, convertible to value_type
bormand 30.07.2021 20:58 # +1
Без этого не будет работать *foo = 42.
ASD_77 27.07.2021 03:23 # 0
reference at (size_type n);
const_reference at (size_type n) const;
ASD_77 27.07.2021 03:23 # 0
reference operator[](difference_type n) const { return *(*this + n); }
а не реф
grillow1337 27.07.2021 16:47 # 0
Для конст_итераторов reference = const T&
Поскольку reference = typename std::conditional<isConst, const T&, T&>::type
crastinus 29.07.2021 13:57 # +2
crastinus 29.07.2021 14:35 # +2
https://coliru.stacked-crooked.com/a/4e5c0e21de62f769