結果
問題 |
No.3073 Fraction Median
|
ユーザー |
![]() |
提出日時 | 2025-03-24 15:01:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 915 ms / 2,500 ms |
コード長 | 1,338 bytes |
コンパイル時間 | 487 ms |
コンパイル使用メモリ | 46,976 KB |
実行使用メモリ | 7,476 KB |
最終ジャッジ日時 | 2025-03-24 15:02:07 |
合計ジャッジ時間 | 14,999 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:45:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 45 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:46:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 46 | for (int i = 0; i < n; i++) scanf("%d", as + i); | ~~~~~^~~~~~~~~~~~~~
ソースコード
/* -*- 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 = 55; const int INF = 1 << 30; /* typedef */ using ll = long long; /* global variables */ int as[MAX_N]; /* subroutines */ bool calc(int n, ll k, double f) { ll sum = 0; for (int i = n - 1, j = n; i >= 0 && j > 0; i--) { double af = as[i] * f; while (j > 0 && as[j - 1] > af) j--; if (j == 0) break; sum += (i < j) ? j - 1 : j; if (sum >= k) return true; } return false; } /* 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; if (calc(n, k, f)) f1 = f; else f0 = 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; }