fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll n,m,a[100005],h=1,c= 1;
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  8. freopen("TROCHOI.inp", "r", stdin);
  9. freopen("TROCHOI.out", "w", stdout);
  10. cin >> n >> m;
  11. for(int i = 1; i <= n*m; i++)
  12. {
  13. cin >> a[i];
  14. }
  15. sort(a+1,a+n*m+1);
  16. ll i = 1;
  17. while(h <= n)
  18. {
  19. if(h&1)
  20. {
  21. for(int j = i; j <= i + m-1; j++)
  22. cout << a[j] << ' ';
  23. cout << '\n';
  24. }
  25. else
  26. {
  27. for(int j = i + m-1; j >= i; j--)
  28. cout << a[j] << ' ';
  29. cout << '\n';
  30. }
  31. i = i + m;
  32. h++;
  33. }
  34.  
  35. }
  36.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty