結果

問題 No.3073 Fraction Median
ユーザー tnakao0123
提出日時 2025-03-24 14:24:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,364 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 46,972 KB
実行使用メモリ 14,684 KB
最終ジャッジ日時 2025-03-24 14:24:56
合計ジャッジ時間 6,383 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 TLE * 1 -- * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:46:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:47:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3073.cc:  No.3073 Fraction Median - yukicoder
 */

#include<cstdio>
#include<algorithm>
#include<numeric>

using namespace std;

/* constant */

const int MAX_N = 1500000;
const int CNT = 70;
const int INF = 1 << 30;

/* typedef */

using ll = long long;

/* global variables */

int as[MAX_N];

/* subroutines */

ll calc(int n, double f) {
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int j = n;
    if (as[i] * f < INF) {
      int x = (int)(as[i] * f);
      j = upper_bound(as, as + n, x) - as;
    }
    if (j > i) j--;
    sum += j;
  }
  return sum;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  sort(as, as + n);

  ll k = (ll)n * (n - 1) / 2;
  double f0 = 0.0, f1 = 1e9 + 1;
  for (int cnt = 0; cnt < CNT; cnt++) {
    double f = (f0 + f1) / 2;
    ll c = calc(n, f);
    //printf(" f=%lf, c=%lld\n", f, c);
    if (c < k) f0 = f;
    else f1 = f;
  }
  //printf(" f1=%lf\n", f1);

  int x = 0, y = 1;
  for (int i = 0; i < n; i++) {
    int fi = (int)(as[i] * f1);
    int j = upper_bound(as, as + n, fi) - as;
    //printf(" i=%d, j=%d\n", i, j);
    while (--j >= 0)
      if (i != j) {
	if ((ll)as[j] * y > (ll)x * as[i]) x = as[j], y = as[i];
	break;
      }
  }

  int g = gcd(x, y);
  x /= g, y /= g;

  printf("%d %d\n", x, y);
  
  return 0;
}
0