結果

問題 No.1294 マウンテン数列
ユーザー 00 Sakuda00 Sakuda
提出日時 2024-04-01 14:27:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 214,704 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-01 14:27:23
合計ジャッジ時間 4,875 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 4 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 24 ms
6,548 KB
testcase_06 AC 33 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 269 ms
6,548 KB
testcase_11 AC 270 ms
6,548 KB
testcase_12 AC 268 ms
6,548 KB
testcase_13 AC 258 ms
6,548 KB
testcase_14 AC 212 ms
6,548 KB
testcase_15 AC 255 ms
6,548 KB
testcase_16 AC 213 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
#include <atcoder/modint>
using namespace atcoder;
using namespace std;
using ll = long long;
using mint = modint998244353;
void naive(vector<int> A) {
	vector<int> B;
	for (int i = 1;i < A.size();i++) B.push_back(A[i]);
	sort(B.begin(), B.end());
	do {
		int d = 0;
		
		for (int i = 0;i < B.size() - 1;i++) d = max(d, abs(B[i + 1] - B[i]));
		for (auto v : B) cout << v << " ";
		cout << " D " << d << endl;
	}while (next_permutation(B.begin(), B.end()));
}
int main() {
	int N;cin >> N;
	int MA = 2500;
	vector<int> A(N + 1, MA + 100000);
	for (int i = 1;i <= N;i++) cin >> A[i];
	sort(A.begin(), A.end(), greater<int>());
	mint ans = 0;
	vector<mint> now(N + 1, 0);
	for (int d = 1;d <= MA;d++) {
		vector<mint> dp(N + 1, 0);
		if (A[1] - A[2] <= d) {
			dp[1] = 2;
		} else {
			continue;
		}
		vector<mint> S(N + 1, 0);
		S[1] = 2;

		for (int i = 2;i < N;i++) {
			if (A[i] - A[i + 1] > d) {
				for (int k = 1;k <= N;k++) dp[k] = 0;
				break;
			}
			int ok = i + 1;
			int ng = 0;
			while (ok - ng  > 1) {
				int m = (ok + ng) / 2;
				if (A[m] - A[i + 1] <= d) {
					ok = m;
				} else {
					ng = m;
				}
			}
			
			dp[i] = S[i - 1] - S[ok - 1];
			S[i] = S[i - 1] + dp[i];
		}
		for (int k = 1;k <= N;k++) ans += mint(d) * mint(dp[k] - now[k]);
		swap(now, dp);
	}
	cout << ans.val() << endl;
}
0