Problem p03379 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define INF 999999999

using namespace std;

typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

llong pow(int x, int n) {
  llong ans = x;
  if (n == 0)
    return 1;

  for (int i = 0; i < n - 1; i++) {
    ans *= x;
  }

  return ans;
}

void swap(int *X, int *Y) {
  int t = *X;
  *X = *Y;
  *Y = t;
}

int main() {
  int n;
  llong tmp;
  vector<llong> X1, X2;
  cin >> n;
  if (n == 2) {
    int a, b;
    cin >> a >> b;
    cout << b << endl;
    cout << a << endl;

    return 0;
  }
  rep(i, n) {
    cin >> tmp;
    X1.push_back(tmp);
    X2.push_back(tmp);
  }

  sort(X2.begin(), X2.end());

  vector<llong>::iterator itr;
  llong ll = X2[(n / 2) - 1], ul = X2[n / 2];
  for (int i = 0; i < X1.size(); ++i) {
    itr = find(X2.begin(), X2.end(), X1[i]);
    if (itr - X2.begin() < n / 2)
      cout << ul << endl;
    else
      cout << ll << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define INF 999999999

using namespace std;

typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

llong pow(int x, int n) {
  llong ans = x;
  if (n == 0)
    return 1;

  for (int i = 0; i < n - 1; i++) {
    ans *= x;
  }

  return ans;
}

void swap(int *X, int *Y) {
  int t = *X;
  *X = *Y;
  *Y = t;
}

int main() {
  int n;
  llong tmp;
  vector<llong> X1, X2;
  cin >> n;
  if (n == 2) {
    int a, b;
    cin >> a >> b;
    cout << b << endl;
    cout << a << endl;

    return 0;
  }
  rep(i, n) {
    cin >> tmp;
    X1.push_back(tmp);
    X2.push_back(tmp);
  }

  sort(X2.begin(), X2.end());

  vector<llong>::iterator itr;
  llong ll = X2[(n / 2) - 1], ul = X2[n / 2];
  for (int i = 0; i < X1.size(); ++i) {
    if (X1[i] <= ll)
      cout << ul << endl;
    else
      cout << ll << endl;
  }

  return 0;
}