結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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);

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

  double 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 << func(a, g, lo) << endl;
  
  return 0;
}
0