Problem p03585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int MAX_N = 100005;
const double eps = 1e-9;

typedef long long i64;

int N, A[MAX_N], B[MAX_N], C[MAX_N], id[MAX_N];

bool cmp(int x, int y) {
  // return 1.0 * A[x] / B[x] < 1.0 * A[y] / B[y];
  int flag = 1;
  if (B[x] < 0)
    flag = -flag;
  if (B[y] < 0)
    flag = -flag;
  return 1LL * flag * A[x] * B[y] < 1LL * flag * A[y] * B[x];
}

double P[MAX_N];

bool cmp2(int x, int y) { return P[x] < P[y]; }

int val[MAX_N], sum[MAX_N], idx[MAX_N];

void update(int x, int v) {
  for (; x <= N; x += x & -x)
    sum[x] += v;
}

int ask(int x) {
  int res = 0;
  for (; x; x -= x & -x)
    res += sum[x];
  return res;
}

i64 calc(double mid) {

  for (int i = 1; i <= N; ++i) {
    P[i] = (C[id[i]] - mid * A[id[i]]) / B[id[i]];
    idx[i] = i;
  }

  sort(idx + 1, idx + N + 1, cmp2);

  memset(sum, 0, sizeof sum);
  i64 num = 0;

  for (int i = 1; i <= N; ++i) {
    num += ask(N - idx[i] + 1);
    update(N - idx[i] + 1, 1);
  }

  return num;
}

double work() {
  double l = -300000000, r = 300000000;

  for (int i = 1; i <= N; ++i)
    id[i] = i;
  sort(id + 1, id + N + 1, cmp);

  i64 tot = 1LL * N * (N - 1) / 2;

  while (r - l > eps) {
    double mid = (l + r) / 2;
    if (calc(mid) * 2 < tot)
      l = mid;
    else
      r = mid;
  }
  return l;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d%d%d", &A[i], &B[i], &C[i]);

  double X = work();

  for (int i = 1; i <= N; ++i)
    swap(A[i], B[i]);
  double Y = work();

  printf("%.10lf %.10lf\n", X, Y);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int MAX_N = 100005;
const double eps = 1e-9;

typedef long long i64;

int N, A[MAX_N], B[MAX_N], C[MAX_N], id[MAX_N];

bool cmp(int x, int y) {
  // return 1.0 * A[x] / B[x] < 1.0 * A[y] / B[y];
  int flag = 1;
  if (B[x] < 0)
    flag = -flag;
  if (B[y] < 0)
    flag = -flag;
  return 1LL * flag * A[x] * B[y] < 1LL * flag * A[y] * B[x];
}

double P[MAX_N];

bool cmp2(int x, int y) { return P[x] < P[y]; }

int val[MAX_N], sum[MAX_N], idx[MAX_N];

void update(int x, int v) {
  for (; x <= N; x += x & -x)
    sum[x] += v;
}

int ask(int x) {
  int res = 0;
  for (; x; x -= x & -x)
    res += sum[x];
  return res;
}

i64 calc(double mid) {

  for (int i = 1; i <= N; ++i) {
    P[i] = (C[id[i]] - mid * A[id[i]]) / B[id[i]];
    idx[i] = i;
  }

  sort(idx + 1, idx + N + 1, cmp2);

  memset(sum, 0, sizeof sum);
  i64 num = 0;

  for (int i = 1; i <= N; ++i) {
    num += ask(N - idx[i] + 1);
    update(N - idx[i] + 1, 1);
  }

  return num;
}

double work() {
  double l = -300000000, r = 300000000;

  for (int i = 1; i <= N; ++i)
    id[i] = i;
  sort(id + 1, id + N + 1, cmp);

  i64 tot = 1LL * N * (N - 1) / 2;

  for (int i = 0; i <= 70; ++i) {
    double mid = (l + r) / 2;
    if (calc(mid) * 2 < tot)
      l = mid;
    else
      r = mid;
  }
  return l;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d%d%d", &A[i], &B[i], &C[i]);

  double X = work();

  for (int i = 1; i <= N; ++i)
    swap(A[i], B[i]);
  double Y = work();

  printf("%.10lf %.10lf\n", X, Y);

  return 0;
}