結果

問題 No.731 等差数列がだいすき
ユーザー koyoprokoyopro
提出日時 2019-12-22 00:51:45
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,982 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 146,300 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-11 07:27:49
合計ジャッジ時間 10,505 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

typedef pair<int, int> pos;
int pos::*x = &pos::first;
int pos::*y = &pos::second;

int dxy[] = {0, 1, 0, -1, 0};

/*================================*/

int N;
float A[1000];

double cost(double b, double d) {
    double diff = 0;
    REP(i, N) {
        double an = b + d * i;
        diff += pow(A[i] - an, 2);
    }
    return diff;
}

signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin >> N;
    REP(i, N) cin >> A[i];
    
    double b = 0, d = 0, c = FLT_MAX;
    
    double base = pow(10, 5);
    REP(ip, 2) REP(i, 25) REP(jp, 2) REP(j, 25) {
        double tb = 1.0 * base / (1<<i);
        if (ip == 1) tb *= -1;
        double td = 1.0 * base / (1<<j);
        if (jp == 1) td *= -1;
        double tc = cost(tb, td);
        if (tc < c) {
            b = tb;
            d = td;
            c = tc;
        }
    }
    
    float diff = base;
    REP(i, base) {
        REP(j, 2) {
            double tb = b + pow(-1, j) * diff;
            double tc = cost(tb, d);
            if (tc < c) {
                b = tb;
                c = tc;
            }
        }
        REP(j, 2) {
            double td = d + pow(-1, j) * diff;
            double tc = cost(b, td);
            if (tc < c) {
                d = td;
                c = tc;
            }
        }
        diff *= 0.9997;
    }
    
    out("%.14f %.14f\n%.14f\n", b, d, c);
    
    return 0;
}
0