結果

問題 No.913 木の燃やし方
ユーザー square1001square1001
提出日時 2020-02-13 21:52:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,146 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 108,788 KB
実行使用メモリ 18,068 KB
最終ジャッジ日時 2024-10-06 06:31:13
合計ジャッジ時間 13,546 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 6 ms
5,248 KB
testcase_04 AC 5 ms
5,248 KB
testcase_05 AC 6 ms
5,248 KB
testcase_06 AC 4 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 311 ms
17,500 KB
testcase_09 AC 295 ms
15,928 KB
testcase_10 AC 300 ms
16,944 KB
testcase_11 WA -
testcase_12 AC 322 ms
17,748 KB
testcase_13 AC 331 ms
17,368 KB
testcase_14 AC 317 ms
15,932 KB
testcase_15 AC 312 ms
16,816 KB
testcase_16 AC 238 ms
17,496 KB
testcase_17 AC 234 ms
15,792 KB
testcase_18 AC 228 ms
16,940 KB
testcase_19 AC 246 ms
17,116 KB
testcase_20 AC 250 ms
17,880 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 259 ms
16,328 KB
testcase_25 AC 231 ms
18,024 KB
testcase_26 AC 331 ms
16,360 KB
testcase_27 AC 297 ms
17,260 KB
testcase_28 AC 303 ms
17,084 KB
testcase_29 AC 302 ms
17,836 KB
testcase_30 AC 294 ms
18,008 KB
testcase_31 AC 283 ms
17,332 KB
testcase_32 AC 289 ms
17,268 KB
testcase_33 AC 289 ms
18,068 KB
testcase_34 AC 277 ms
17,244 KB
testcase_35 AC 270 ms
17,880 KB
testcase_36 AC 244 ms
17,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <set>
#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 62;
vector<long long> subsolve(int N, vector<long long> A) {
	A.insert(A.begin(), 0);
	for (int i = 0; i < N; ++i) {
		A[i + 1] += A[i];
	}
	// minimize (A[i] - A[j]) + (i - j)^2 = (A[i] + i * i) + (-A[j] + j * j) - 2 * j * i for each i
	vector<long long> intercept(N + 1), slope(N + 1);
	for (int i = 0; i <= N; ++i) {
		intercept[i] = -A[i] + 1LL * i * i;
		slope[i] = -2 * i;
	}
	int mid = N / 2 + 1;
	vector<long long> seq(N, inf);
	deque<int> que;
	for (int i = 0; i < mid; ++i) {
		while (que.size() >= 2) {
			int pa = que[que.size() - 2];
			int pb = que[que.size() - 1];
			__int128 cra = __int128(intercept[pa] - intercept[i]) * (i - pb);
			__int128 crb = __int128(intercept[pb] - intercept[i]) * (i - pa);
			if (cra < crb) que.pop_back();
			else break;
		}
		que.push_back(i);
		while (que.size() >= 2) {
			int pa = que[0];
			int pb = que[1];
			long long va = 1LL * slope[pa] * (i + 1) + intercept[pa];
			long long vb = 1LL * slope[pb] * (i + 1) + intercept[pb];
			if (va > vb) que.pop_front();
			else break;
		}
	}
	for (int i = mid; i <= N; ++i) {
		seq[i - 1] = que[0];
		while (que.size() >= 2) {
			int pa = que[que.size() - 2];
			int pb = que[que.size() - 1];
			__int128 cra = __int128(intercept[pa] - intercept[i]) * (i - pb);
			__int128 crb = __int128(intercept[pb] - intercept[i]) * (i - pa);
			if (cra < crb) que.pop_back();
			else break;
		}
		while (que.size() >= 2) {
			int pa = que[0];
			int pb = que[1];
			long long va = 1LL * slope[pa] * (i + 1) + intercept[pa];
			long long vb = 1LL * slope[pb] * (i + 1) + intercept[pb];
			if (va > vb) que.pop_front();
			else break;
		}
	}
	for (int i = 0; i < N; ++i) {
		if (seq[i] != inf) {
			seq[i] = A[i + 1] - A[seq[i]] + 1LL * (i + 1 - seq[i]) * (i + 1 - seq[i]);
		}
	}
	for (int i = N - 2; i >= mid - 1; --i) {
		seq[i] = min(seq[i], seq[i + 1]);
	}
	return seq;
}
vector<long long> solve(int N, vector<long long> A) {
	if (N == 1) return vector<long long>({ A[0] + 1 });
	int mid = N / 2;
	vector<long long> res1 = subsolve(N, A);
	reverse(A.begin(), A.end());
	vector<long long> res2 = subsolve(N, A);
	reverse(res2.begin(), res2.end());
	for (int i = 0; i < N; ++i) {
		res1[i] = min(res1[i], res2[i]);
	}
	reverse(A.begin(), A.end());
	vector<long long> res3 = solve(mid, vector<long long>(A.begin(), A.begin() + mid));
	vector<long long> res4 = solve(N - mid, vector<long long>(A.begin() + mid, A.end()));
	for (int i = 0; i < mid; ++i) {
		res1[i] = min(res1[i], res3[i]);
	}
	for (int i = mid; i < N; ++i) {
		res1[i] = min(res1[i], res4[i - mid]);
	}
	return res1;
}
vector<long long> solve_easy(int N, vector<long long> A) {
	vector<long long> ans(N, 1LL << 62);
	for (int i = 0; i < N; ++i) {
		for (int j = i + 1; j <= N; ++j) {
			long long sum = 1LL * (j - i) * (j - i);
			for (int k = i; k < j; ++k) {
				sum += A[k];
			}
			for (int k = i; k < j; ++k) {
				ans[k] = min(ans[k], sum);
			}
		}
	}
	return ans;
}
#include <random>
#include <string>
using namespace std;
mt19937 mt(1910182301);
int rand_rng(int l, int r) {
	uniform_int_distribution<int> p(l, r - 1);
	return p(mt);
}
string stringify(vector<long long> A) {
	string res = "[";
	for (int i = 0; i < A.size(); ++i) {
		if (i) res += ", ";
		res += to_string(A[i]);
	}
	res += "]";
	return res;
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<long long> A(N);
	for (int i = 0; i < N; ++i) cin >> A[i];
	vector<long long> ans = solve(N, A);
	for (int i = 0; i < N; ++i) {
		cout << ans[i] << '\n';
	}
	/*
	int N = 3;
	for (int i = 1; i <= 10000; ++i) {
		vector<long long> A(N);
		for (int j = 0; j < N; ++j) {
			A[j] = rand_rng(-6, 7);
		}
		vector<long long> res1 = solve(N, A);
		vector<long long> res2 = solve_easy(N, A);
		if (res1 != res2) {
			cout << "Case #" << i << ":" << endl;
			cout << "N = " << N << ", A = " << stringify(A) << endl;
			cout << "Returns: " << stringify(res1) << endl;
			cout << "Answer: " << stringify(res2) << endl;
			cout << endl;
		}
	}
	*/
	return 0;
}
0