結果

問題 No.731 等差数列がだいすき
ユーザー polylogKpolylogK
提出日時 2018-09-07 22:07:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,405 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 170,260 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-07 01:57:50
合計ジャッジ時間 6,488 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 69 ms
5,376 KB
testcase_04 AC 186 ms
5,376 KB
testcase_05 AC 70 ms
5,376 KB
testcase_06 AC 264 ms
5,376 KB
testcase_07 AC 147 ms
5,376 KB
testcase_08 AC 180 ms
5,376 KB
testcase_09 AC 229 ms
5,376 KB
testcase_10 AC 277 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 125 ms
5,376 KB
testcase_13 AC 171 ms
5,376 KB
testcase_14 AC 203 ms
5,376 KB
testcase_15 AC 251 ms
5,376 KB
testcase_16 AC 135 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 311 ms
5,376 KB
testcase_20 AC 313 ms
5,376 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 lim) {
	double left, right;
	if(b < 0) left = 0.0, right = lim;
	else left = -lim, right = lim;
	
	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 lim) {
	double left = -1e6, right = 1e6;
	
	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, lim)) <= cost(mr, solve_D(mr, lim))) {
			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(1e5);
	double d = solve_D(b, 1e5);
	double c = cost(b, d);
	double B = solve_B(4e5);
	double D = solve_D(B, 4e5);
	double C = cost(B, D);
	cout << std::fixed << std::setprecision(16);
	if(c < C) cout << b << " " << d << '\n' << c << endl;
	else cout << B << " " << D << '\n' << C << endl;
	return 0;
}
0