Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

Блог пользователя zkzk_-

Автор zkzk_-, история, 2 года назад, По-английски
Given an array of integers, our goal is to rearrange its elements so that all odd numbers are on the left side and all even numbers are on the right side, while keeping the relative order of elements unchanged.
 How would you do?
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Make an auxiliary array, and loop over the given array twice.

Get all the odds on the first round and all the evens on the second.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

I would use STL:

std::stable_partition(a.begin(), a.end(), [](int x) { return x % 2 != 0; });

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Use stable partition. Stable partition rearranges in True first order. So your condition of partition should be i % 2. stable_partition(all(A), [] (int i) {return i % 2;});