結果

問題 No.731 等差数列がだいすき
ユーザー kk
提出日時 2020-09-15 23:03:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 1,500 ms
コード長 997 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 200,664 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 03:05:29
合計ジャッジ時間 3,603 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 21 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 31 ms
4,380 KB
testcase_07 AC 18 ms
4,376 KB
testcase_08 AC 23 ms
4,380 KB
testcase_09 AC 28 ms
4,376 KB
testcase_10 AC 33 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 19 ms
4,380 KB
testcase_14 AC 22 ms
4,376 KB
testcase_15 AC 27 ms
4,380 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 34 ms
4,380 KB
testcase_18 AC 35 ms
4,376 KB
testcase_19 AC 36 ms
4,376 KB
testcase_20 AC 35 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

__float128 func(const vector<int> &a, double g, double d) {
  int n = a.size();
  __float128 x = g - d * (n-1) / 2;

  __float128 ret = 0;
  REP (i, n) {
    ret += (a[i] - x) * (a[i] - x);
    x += d;
  }
  
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  REP (i, n) cin >> a[i];

  double g = 0.0;
  REP (i, n) g += a[i];
  g /= n;

  double lo = -1e18;
  double hi = 1e18;

  while (hi - lo > 1e-9) {
    double m1 = (2 * lo + hi) / 3;
    double m2 = (lo + 2 * hi) / 3;

    if (func(a, g, m1) < func(a, g, m2))
      hi = m2;
    else
      lo = m1;
  }

  cout << fixed << setprecision(12);
  cout << g - lo * (n-1)/2 << " " << lo << endl;
  cout << (double)func(a, g, lo) << endl;
  
  return 0;
}
0