Problem p03223 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int n;
  scanf("%d", &n);
  VLL a(n);
  REP(i, n) scanf("%lld", &a[i]);
  sort(ALL(a));
  LL x[2] = {a[0], a[0]};
  int l = 1, r = n - 1;
  LL ans = 0;
  while (l <= r) {
    VLL d(4);
    d[0] = abs(a[l] - x[0]);
    d[1] = abs(a[r] - x[0]);
    d[2] = abs(a[l] - x[1]);
    d[3] = abs(a[r] - x[1]);
    int maxidx = -1;
    LL maxd = 0;
    REP(i, 4) {
      if (maxd < d[i]) {
        maxd = d[i];
        maxidx = i;
      }
    }
    ans += maxd;
    if (maxidx == 0)
      x[0] = a[l++];
    else if (maxidx == 1)
      x[0] = a[r--];
    else if (maxidx == 2)
      x[1] = a[l++];
    else if (maxidx == 3)
      x[1] = a[r--];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int n;
  scanf("%d", &n);
  VLL a(n);
  REP(i, n) scanf("%lld", &a[i]);
  sort(ALL(a));
  LL x[2] = {a[0], a[0]};
  int l = 1, r = n - 1;
  LL ans = 0;
  while (l <= r) {
    VLL d(4);
    d[0] = abs(a[l] - x[0]);
    d[1] = abs(a[r] - x[0]);
    d[2] = abs(a[l] - x[1]);
    d[3] = abs(a[r] - x[1]);
    int maxidx = -1;
    LL maxd = -1;
    REP(i, 4) {
      if (maxd < d[i]) {
        maxd = d[i];
        maxidx = i;
      }
    }
    ans += maxd;
    if (maxidx == 0)
      x[0] = a[l++];
    else if (maxidx == 1)
      x[0] = a[r--];
    else if (maxidx == 2)
      x[1] = a[l++];
    else if (maxidx == 3)
      x[1] = a[r--];
  }
  cout << ans << endl;
  return 0;
}