Problem p02861 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;

#define Love_coding int main
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define fast ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define fde(i, p, n) for (long long int i = p; i >= n; i--)
#define f(i, p, n) for (long long int i = p; i < n; i++)
#define pii pair<int, int>
#define pf p.first
#define ps p.second
// int N,M;

// int Dp[101][101]={-1};
// int tc[101][101];
int h = 101;
#define chi(p, r) p - r;

#define ll long long int

/*
struct arr{

int x;
string y;
};

*/

int comparator(const void *a, const void *b) { return (*(int *)a - *(int *)b); }

long long int C(long long int n, long long int r) {
  long long int res = 1, i;
  if (r > n / 2)
    r = n - r;
  for (i = 0; i < r; i++) {
    res *= n;
    res /= i + 1;
    n--;
  }
  return res;
}

unsigned nCk(unsigned n, unsigned k) {
  if (k > n)
    return 0;
  if (k * 2 > n)
    k = n - k;
  if (k == 0)
    return 1;

  int result = n;
  for (int i = 2; i <= k; ++i) {
    result *= (n - i + 1);
    result /= i;
  }
}

struct node {
  int x;
  int ind;
  struct node *p;
};

void max_dis() {
  int n;
  cin >> n;
  int a[n];
  trace(n);

  f(i, 0, n) { cin >> a[i]; }

  int chhote = 0;

  f(i, 0, n) cout << a[i] << " ";

  cout << "\n";

  node *h = new node[30000];

  f(i, 0, n) { h[i].p = NULL; }

  f(i, 0, n) {
    cout << "entered  " << i << "\n";

    if (h[a[i]].p == NULL) {

      h[a[i]].ind = i;
      h[a[i]].x = a[i];
      h[a[i]].p = NULL;

    }

    else {
      node *root = NULL;
      root = &h[a[i]];

      while (root) {
        root = root->p;
      }

      node *l = new node();
      l->ind = i;
      l->x = h[a[i]].x;
      l->p = NULL;
      root->p = l;
    }
  }

  int j = 0;

  f(i, 0, n) {
    node *root;
    root = &h[a[i]];

    int gore = 0;
    gore = i;

    while (root != NULL && j < 1000) {
      cout << a[i] << " " << root->ind << " " << root->x << "\n";

      int first = root->ind;
      root = root->p;

      int second = root->ind;

      if (second - first > chhote) {
        chhote = second - first;
      }
      j++;
    }
  }
  delete[] h;

  if (chhote >= 1)
    cout << chhote + 1 << "\n";

  else
    cout << -1 << "\n";
}

void jodo(int a[], int left, int right) {
  int mid = left + (right - left) / 2;

  if (left < right) {
    jodo(a, mid + 1, right);
    jodo(a, left, mid);
  }

  if (left == right)
    return;

  int b[right - left + 1];

  int j = left, k = mid + 1;

  f(i, 0, right - left + 1) {
    if (j < mid + 1 && k < right + 1) {
      if (a[j] < a[k]) {
        b[i] = a[j];
        j++;
      }

      else {
        b[i] = a[k];
        k++;
      }

    }

    else if (j == mid + 1 && k < right + 1) {
      b[i] = a[k];
      k++;
    }

    else if (j < mid + 1 && k == right + 1) {
      b[i] = a[j];
      j++;
    }

    else {
      cout << "my bad  " << i << " " << left << "  " << mid << "  j: " << j
           << "  " << mid + 1 << "  " << right << "  k: " << k << "  "
           << "\n";
    }
  }

  int t = left;

  f(i, 0, right - left + 1) { a[i + t] = b[i]; }
}

void mer() {
  int n;
  cin >> n;

  int a[n];

  f(i, 0, n) cin >> a[i];

  int left = 0, right = n - 1;

  jodo(a, left, right);

  cout << "\n";

  f(i, 0, n) cout << a[i] << " ";
}

void gor() {

  int n;
  cin >> n;

  string s;
  cin >> s;
  vector<char> v1, v2;

  f(i, 0, n) {
    if (s[i] >= 65 && s[i] <= 90) {
      v1.push_back(s[i]);
    }

    else {
      v2.push_back(s[i]);
    }
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  string q = "";

  /*
for(auto i=0;i<v1.size();i++)
{
  h+=v1[i];

}
*/

  int j = 0, k = 0;
  f(i, 0, n) {

    if (s[i] >= 65 && s[i] <= 90) {
      q.push_back(v1[j]);
      j++;

    }

    else {
      q.push_back(v2[k]);
      k++;
    }
  }

  cout << q << "\n";
}

int N, M;
int k[1001][1001];
int H[1001][1001];

int mx = 0;

int jolly(int i, int j, int sum) {
  if (i > N || j > M)
    return sum;

  if (H[i][j] != 0) {
    return H[i][j];
  }

  if (i == N && j == M) {
    return sum + k[N][M];
  }

  H[i][j] = max(
      jolly(i + 1, j, sum + k[i][j]),
      max(jolly(i, j + 1, sum + k[i][j]), jolly(i + 1, j + 1, sum + k[i][j])));

  return H[i][j];
}

void coll() {

  cin >> N >> M;

  f(i, 1, N + 1) {
    f(j, 1, M + 1) {
      cin >> k[i][j];
      H[i][j] = 0;
    }
  }

  int i = 0, j = 0, sum = 0;

  cout << jolly(i, j, sum) << "\n";
}

void fp() {
  int N;
  cin >> N;

  int a[N][2];

  f(i, 0, N) { cin >> a[i][0] >> a[i][1]; }

  double sum = 0;

  f(i, 0, N) {
    f(j, i, N) {
      double p =
          sqrt(pow((a[i][0] - a[j][0]), 2) + pow((a[i][1] - a[j][1]), 2));

      sum += p;

      //  trace(p);
    }
  }

  cout << setprecision(15) << sum * (2 / (double)N);
}

Love_coding() {
#ifndef ONLINE_JUDGE
  freopen("test2.txt", "r", stdin);
#endif

  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    fp();
  }

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define Love_coding int main
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define fast ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define fde(i, p, n) for (long long int i = p; i >= n; i--)
#define f(i, p, n) for (long long int i = p; i < n; i++)
#define pii pair<int, int>
#define pf p.first
#define ps p.second
// int N,M;

// int Dp[101][101]={-1};
// int tc[101][101];
int h = 101;
#define chi(p, r) p - r;

#define ll long long int

/*
struct arr{

int x;
string y;
};

*/

int comparator(const void *a, const void *b) { return (*(int *)a - *(int *)b); }

long long int C(long long int n, long long int r) {
  long long int res = 1, i;
  if (r > n / 2)
    r = n - r;
  for (i = 0; i < r; i++) {
    res *= n;
    res /= i + 1;
    n--;
  }
  return res;
}

unsigned nCk(unsigned n, unsigned k) {
  if (k > n)
    return 0;
  if (k * 2 > n)
    k = n - k;
  if (k == 0)
    return 1;

  int result = n;
  for (int i = 2; i <= k; ++i) {
    result *= (n - i + 1);
    result /= i;
  }
}

struct node {
  int x;
  int ind;
  struct node *p;
};

void max_dis() {
  int n;
  cin >> n;
  int a[n];
  trace(n);

  f(i, 0, n) { cin >> a[i]; }

  int chhote = 0;

  f(i, 0, n) cout << a[i] << " ";

  cout << "\n";

  node *h = new node[30000];

  f(i, 0, n) { h[i].p = NULL; }

  f(i, 0, n) {
    cout << "entered  " << i << "\n";

    if (h[a[i]].p == NULL) {

      h[a[i]].ind = i;
      h[a[i]].x = a[i];
      h[a[i]].p = NULL;

    }

    else {
      node *root = NULL;
      root = &h[a[i]];

      while (root) {
        root = root->p;
      }

      node *l = new node();
      l->ind = i;
      l->x = h[a[i]].x;
      l->p = NULL;
      root->p = l;
    }
  }

  int j = 0;

  f(i, 0, n) {
    node *root;
    root = &h[a[i]];

    int gore = 0;
    gore = i;

    while (root != NULL && j < 1000) {
      cout << a[i] << " " << root->ind << " " << root->x << "\n";

      int first = root->ind;
      root = root->p;

      int second = root->ind;

      if (second - first > chhote) {
        chhote = second - first;
      }
      j++;
    }
  }
  delete[] h;

  if (chhote >= 1)
    cout << chhote + 1 << "\n";

  else
    cout << -1 << "\n";
}

void jodo(int a[], int left, int right) {
  int mid = left + (right - left) / 2;

  if (left < right) {
    jodo(a, mid + 1, right);
    jodo(a, left, mid);
  }

  if (left == right)
    return;

  int b[right - left + 1];

  int j = left, k = mid + 1;

  f(i, 0, right - left + 1) {
    if (j < mid + 1 && k < right + 1) {
      if (a[j] < a[k]) {
        b[i] = a[j];
        j++;
      }

      else {
        b[i] = a[k];
        k++;
      }

    }

    else if (j == mid + 1 && k < right + 1) {
      b[i] = a[k];
      k++;
    }

    else if (j < mid + 1 && k == right + 1) {
      b[i] = a[j];
      j++;
    }

    else {
      cout << "my bad  " << i << " " << left << "  " << mid << "  j: " << j
           << "  " << mid + 1 << "  " << right << "  k: " << k << "  "
           << "\n";
    }
  }

  int t = left;

  f(i, 0, right - left + 1) { a[i + t] = b[i]; }
}

void mer() {
  int n;
  cin >> n;

  int a[n];

  f(i, 0, n) cin >> a[i];

  int left = 0, right = n - 1;

  jodo(a, left, right);

  cout << "\n";

  f(i, 0, n) cout << a[i] << " ";
}

void gor() {

  int n;
  cin >> n;

  string s;
  cin >> s;
  vector<char> v1, v2;

  f(i, 0, n) {
    if (s[i] >= 65 && s[i] <= 90) {
      v1.push_back(s[i]);
    }

    else {
      v2.push_back(s[i]);
    }
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  string q = "";

  /*
for(auto i=0;i<v1.size();i++)
{
  h+=v1[i];

}
*/

  int j = 0, k = 0;
  f(i, 0, n) {

    if (s[i] >= 65 && s[i] <= 90) {
      q.push_back(v1[j]);
      j++;

    }

    else {
      q.push_back(v2[k]);
      k++;
    }
  }

  cout << q << "\n";
}

int N, M;
int k[1001][1001];
int H[1001][1001];

int mx = 0;

int jolly(int i, int j, int sum) {
  if (i > N || j > M)
    return sum;

  if (H[i][j] != 0) {
    return H[i][j];
  }

  if (i == N && j == M) {
    return sum + k[N][M];
  }

  H[i][j] = max(
      jolly(i + 1, j, sum + k[i][j]),
      max(jolly(i, j + 1, sum + k[i][j]), jolly(i + 1, j + 1, sum + k[i][j])));

  return H[i][j];
}

void coll() {

  cin >> N >> M;

  f(i, 1, N + 1) {
    f(j, 1, M + 1) {
      cin >> k[i][j];
      H[i][j] = 0;
    }
  }

  int i = 0, j = 0, sum = 0;

  cout << jolly(i, j, sum) << "\n";
}

void fp() {
  int N;
  cin >> N;

  int a[N][2];

  f(i, 0, N) { cin >> a[i][0] >> a[i][1]; }

  double sum = 0;

  f(i, 0, N) {
    f(j, i, N) {
      double p =
          sqrt(pow((a[i][0] - a[j][0]), 2) + pow((a[i][1] - a[j][1]), 2));

      sum += p;

      //  trace(p);
    }
  }

  cout << setprecision(15) << sum * (2 / (double)N);
}

Love_coding() {

  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    fp();
  }

  return 0;
}