#include #include #include using namespace std; int main() { int n; cin >> n; vector> v(n); for(int i = 0; i < n; ++i) { v[i].first = i + 1; cin >> v[i].second; } int k = 0; while(k * k < n) ++k; auto key = [&](auto x) { auto first = x.first / k; auto second = (first % 2 == 0) ? x.second : -x.second; return std::pair{first, second}; }; sort(begin(v), end(v), [&](auto x, auto y) { return key(x) < key(y); }); for(auto x : v) cout << x.first << " " << x.second << endl; }