提出 #70605761


ソースコード 拡げる

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n; cin >> n;
	vector<int>x(n + 1);
	rep2(i, 1, n + 1)cin >> x[i];
	long long ans = 2 * INF;
	set<long long>st = { -INF * 2,0,INF * 2 };
	map<int, int>mp;
	mp[0] = 0;
	// 門番が必須
	auto before = [&](long long n)->long long {
		auto ite = prev(st.lower_bound(n));
		return *ite;
	};
	auto after = [&](long long n)->long long {
		auto ite = st.upper_bound(n);
		return *ite;
	};

	rep2(i, 1, n + 1) {
		int b = before(x[i]);
		int a = after(x[i]);
		ans += min(a - x[i], x[i] - b);
		if (abs(b) != INF * 2) {
			int b2 = before(b);
			int a2 = after(b);
			ans -= min(a2 - b, b - b2);
		}
		if (abs(a) != INF * 2) {
			int b2 = before(a);
			int a2 = after(a);
			ans -= min(a2 - a, a - b2);
		}
		st.insert(x[i]);
		if (abs(b) != INF * 2) {
			int b2 = before(b);
			int a2 = after(b);
			ans += min(a2 - b, b - b2);
		}
		if (abs(a) != INF * 2) {
			int b2 = before(a);
			int a2 = after(a);
			ans += min(a2 - a, a - b2);
		}
		cout << ans << endl;
	}
	return 0;
}

提出情報

提出日時
問題 D - Neighbor Distance
ユーザ kwm_t
言語 C++23 (GCC 15.2.0)
得点 400
コード長 1938 Byte
結果 AC
実行時間 850 ms
メモリ 31004 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 1
AC × 31
セット名 テストケース
Sample sample_01.txt
All sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 2 ms 3416 KiB
test_01.txt AC 1 ms 3600 KiB
test_02.txt AC 1 ms 3624 KiB
test_03.txt AC 597 ms 30744 KiB
test_04.txt AC 596 ms 30748 KiB
test_05.txt AC 592 ms 30744 KiB
test_06.txt AC 788 ms 30988 KiB
test_07.txt AC 789 ms 30988 KiB
test_08.txt AC 787 ms 31004 KiB
test_09.txt AC 756 ms 30748 KiB
test_10.txt AC 694 ms 30744 KiB
test_11.txt AC 753 ms 30732 KiB
test_12.txt AC 693 ms 30728 KiB
test_13.txt AC 683 ms 30992 KiB
test_14.txt AC 689 ms 31000 KiB
test_15.txt AC 684 ms 30996 KiB
test_16.txt AC 687 ms 30996 KiB
test_17.txt AC 843 ms 30992 KiB
test_18.txt AC 844 ms 31000 KiB
test_19.txt AC 843 ms 30992 KiB
test_20.txt AC 845 ms 31004 KiB
test_21.txt AC 845 ms 31004 KiB
test_22.txt AC 843 ms 31000 KiB
test_23.txt AC 846 ms 31004 KiB
test_24.txt AC 843 ms 30988 KiB
test_25.txt AC 846 ms 30992 KiB
test_26.txt AC 845 ms 30988 KiB
test_27.txt AC 846 ms 30996 KiB
test_28.txt AC 845 ms 30996 KiB
test_29.txt AC 850 ms 30992 KiB
test_30.txt AC 843 ms 30992 KiB