File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed
Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff 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};
You can’t perform that action at this time.
0 commit comments