Skip to content

Commit d718863

Browse files
committed
Partition Two sorts
1 parent 3e510c2 commit d718863

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

util/partition.hh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,30 @@ struct PartitionTwo {
99
FixedVector<unsigned, MaxElements> b{};
1010

1111
PartitionTwo(std::span<unsigned> vals) {
12+
struct IdVal {
13+
unsigned val;
14+
unsigned id;
15+
};
16+
17+
FixedVector<IdVal, MaxElements> ordered;
18+
for (auto val : vals) {
19+
ordered.push_back({val, (unsigned)ordered.size()});
20+
}
21+
22+
std::ranges::sort(ordered, std::greater{}, &IdVal::val);
23+
1224
unsigned a_sum = 0;
1325
unsigned b_sum = 0;
1426

1527
// The part with the smaller sum gets the next element
16-
for (auto i = 0u; auto val : vals) {
28+
for (auto [val, i] : ordered) {
1729
if (a_sum <= b_sum) {
1830
a_sum += val;
1931
a.push_back(i);
2032
} else {
2133
b_sum += val;
2234
b.push_back(i);
2335
}
24-
i++;
2536
}
2637
}
2738
};

0 commit comments

Comments
 (0)