結果

問題 No.731 等差数列がだいすき
ユーザー ei1333333ei1333333
提出日時 2018-09-07 22:41:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,113 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 209,280 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-07 03:59:09
合計ジャッジ時間 16,258 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,812 KB
testcase_01 AC 12 ms
6,940 KB
testcase_02 AC 14 ms
6,944 KB
testcase_03 AC 253 ms
6,940 KB
testcase_04 AC 686 ms
6,940 KB
testcase_05 AC 257 ms
6,940 KB
testcase_06 AC 984 ms
6,940 KB
testcase_07 AC 552 ms
6,940 KB
testcase_08 AC 678 ms
6,940 KB
testcase_09 AC 848 ms
6,940 KB
testcase_10 AC 1,022 ms
6,944 KB
testcase_11 AC 37 ms
6,944 KB
testcase_12 AC 457 ms
6,940 KB
testcase_13 AC 633 ms
6,944 KB
testcase_14 AC 755 ms
6,940 KB
testcase_15 WA -
testcase_16 AC 498 ms
6,940 KB
testcase_17 AC 1,161 ms
6,944 KB
testcase_18 AC 1,162 ms
6,948 KB
testcase_19 AC 1,166 ms
6,940 KB
testcase_20 AC 1,170 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

int main() {
  int N, A[1000];

  cin >> N;
  for(int i = 0; i < N; i++) cin >> A[i];


  tuple< double, double, double > ret(1e30, 1e30, 1e30);

  {
    double left = -1e10, right = 1e10;

    auto check2 = [&](double real_a) {
      auto check = [&](double real_d) {
        double cost = 0.0;
        for(int j = 0; j < N; j++) {
          double v = real_a + j * real_d;
          cost += (A[j] - v) * (A[j] - v);
        }
        ret = min(ret, make_tuple(cost, real_a, real_d));
        return cost;
      };

      double low = -1e5, high = 1e5;
      for(int j = 0; j < 300; j++) {
        double latte = (low * 2 + high) / 3;
        double malta = (low + high * 2) / 3;
        if(check(latte) < check(malta)) high = malta;
        else low = latte;
      }
      return check(low);
    };


    for(int i = 0; i < 300; i++) {
      double aa = (left * 2 + right) / 3;
      double bb = (left + right * 2) / 3;
      if(check2(aa) < check2(bb)) right = bb;
      else left = aa;
    }
  }


  {
    tuple< double, double, double > ret(1e30, 1e30, 1e30);
    double left = -40000000, right = 40000000;

    auto check2 = [&](double real_d) {
      auto check = [&](double a) {
        double b = a;
        double cost = 0.0;
        for(int j = 0; j < N; j++) {
          cost += (A[j] - a) * (A[j] - a);
          a += real_d;
        }
        ret = min(ret, make_tuple(cost, b, real_d));
        return cost;
      };

      double low = -40000000, high = 40000000;
      for(int j = 0; j < 300; j++) {
        double latte = (low * 2 + high) / 3;
        double malta = (low + high * 2) / 3;
        if(check(latte) < check(malta)) high = malta;
        else low = latte;
      }
      return check(low);
    };

    for(int i = 0; i < 300; i++) {
      double aa = (left * 2 + right) / 3;
      double bb = (left + right * 2) / 3;
      if(check2(aa) < check2(bb)) right = bb;
      else left = aa;
    }
  }


  cout << fixed << setprecision(10) << get< 1 >(ret) << " " << get< 2 >(ret) << endl << get< 0 >(ret) << endl;
}
0