結果

問題 No.731 等差数列がだいすき
ユーザー polylogKpolylogK
提出日時 2018-09-07 22:30:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,187 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 169,272 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 20:15:48
合計ジャッジ時間 4,713 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
typedef long long i64;
using std::cout;
using std::endl;
using std::cin;

std::vector<double> a;
double n;

double cost(double b, double d) {
	double ret = 0.0;
	for(int i = 0; i < n; i++) {
		ret += (a[i] - b) * (a[i] - b);
		b += d;
	}
	
	assert(ret >= 0);
	return ret;
}

double solve_D(double b) {
	double left = -b, right = (1e5 - b) / (n - 1);
	
	for(int i = 0; i < 150; i++) {
		double ml = (left + left + right) / 3.0;
		double mr = (left + right + right) / 3.0;
		
		if(cost(b, ml) <= cost(b, mr)) {
			right = mr;
		} else {
			left = ml;
		}
	}
	
	return (left + right) / 2.0;
}

double solve_B() {
	double left = -1e9, right = 1e9;
	
	for(int i = 0; i < 150; i++) {
		double ml = (left + left + right) / 3.0;
		double mr = (left + right + right) / 3.0;
		
		if(cost(ml, solve_D(ml)) <= cost(mr, solve_D(mr))) {
			right = mr;
		} else {
			left = ml;
		}
	}
	
	return (left + right) / 2.0;
}	

int main(){
	cin >> n; a.resize(n);
	for(int i = 0; i < n; i++) cin >> a[i];
	
	
	double b = solve_B();
	double d = solve_D(b);
	double c = cost(b, d);
	cout << std::fixed << std::setprecision(16);
	cout << b << " " << d << '\n' << c << endl;
	return 0;
}
0