結果

問題 No.707 書道
ユーザー stkchpstkchp
提出日時 2018-07-09 21:58:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,577 bytes
コンパイル時間 1,669 ms
コンパイル使用メモリ 83,352 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 04:27:25
合計ジャッジ時間 1,646 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <iomanip>
#include <cfloat>

using namespace std;

namespace {

template <typename T>
constexpr T sqrt(T s)
{
	T x = s / 2.0;
	T prev = 0.0;

	while (x != prev)
	{
		prev = x;
		x = (x + s / x) / 2.0;
	}
	return x;
}

template<int N> struct Sqrt {
	double ary[N * N];
	constexpr Sqrt() : ary() {
		for (int i = 0; i < N; ++i)
			for (int j = 0; j < N; ++j)
				ary[j + i * (N - 1)] = sqrt(static_cast<double>(i * i + j * j));
	}
};

constexpr int abs(int x) {
	return x < 0 ? -x : x;
}

constexpr auto d_map = Sqrt<52>();

constexpr auto distance(int x1, int y1, int x2, int y2)
{
	return d_map.ary[abs(x1 - x2) + abs(y1 - y2) * 51];
}
} // namespace

int main()
{
	int H, W;
	cin >> H >> W;

	// ポイントを作成
	vector<array<int, 2>> pts;
	for (int y = 1; y <= H; ++y)
	{
		string str;
		cin >> str;
		for (int x = 1; x <= W; ++x)
		{
			if (str.at(x - 1) == '1') pts.push_back({ x, y });
		}
	}

	double minv = LDBL_MAX;
	for (int x = 1; x <= W; ++x)
	{
		double v1 = 0;
		double v2 = 0;
		for (const auto &pt : pts)
		{
			v1 += distance(x, 0, pt.at(0), pt.at(1));
			v2 += distance(x, H + 1, pt.at(0), pt.at(1));
		}
		minv = min(minv, v1);
		minv = min(minv, v2);
	}

	for (int y = 1; y <= H; ++y)
	{
		double v1 = 0;
		double v2 = 0;
		for (const auto &pt : pts)
		{
			v1 += distance(0, y, pt.at(0), pt.at(1));
			v2 += distance(W + 1, y, pt.at(0), pt.at(1));
		}
		minv = min(minv, v1);
		minv = min(minv, v2);
	}

	cout << fixed << setprecision(9) << minv << endl;

	return 0;
}
0