답안 #825520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
825520 2023-08-15T01:06:05 Z NK_ Building Bridges (CEOI17_building) C++17
0 / 100
938 ms 18460 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using ll = long long;
using pl = pair<ll, ll>;
using vl = V<ll>;
// using vpi = V<pi>;

const ll INFL = ll(1e16) + 1008;
const int nax = 1e6 + 6;
const int SQ = 2e3 + 1;
pl oc[nax];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	for(int x = 0; x < nax; x++) {
		oc[x] = mp(INFL, -1);
	}

	int N; cin >> N;
	vi A(N); for(auto& x : A) cin >> x;
	vi W(N); for(auto& x : W) cin >> x;

	vl P = {0}; for(auto& x : W) P.pb(P.back() + x);


	auto query = [&](int l, int r) {
		if (l > r) return 0LL;
		return P[r + 1] - P[l];
	};

	auto sq = [&](int x) { return x * 1LL * x; };

	vl dp(N, INFL); dp[0] = 0;

	set<int> S;

	oc[A[0]] = mp(dp[0] - P[0], 0);
	for(int i = 1; i < N; i++) {
		int x = A[i];

		for(int d = max(-SQ, -x); d <= min(SQ, nax-1-x); d++) {
			int y = x + d; 
			if (oc[y].second == -1) continue;
			int j = oc[y].second; ll cost = sq(x - y) + query(j + 1, i - 1); 
			// cout << i << " " << j << " => " << cost << endl;
			dp[i] = min(dp[i], dp[j] + cost);
		}


		ll c = dp[i] - P[i];
		if (oc[x].first > c) oc[x] = mp(c, i);
	}

	cout << min(dp.back(), sq(A.front() + A.back()) + query(1, N - 1)) << nl;

    return 0;
}			

# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15888 KB Output is correct
2 Correct 7 ms 15944 KB Output is correct
3 Correct 7 ms 15956 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Incorrect 9 ms 15956 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 938 ms 18452 KB Output is correct
2 Incorrect 888 ms 18460 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15888 KB Output is correct
2 Correct 7 ms 15944 KB Output is correct
3 Correct 7 ms 15956 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Incorrect 9 ms 15956 KB Output isn't correct
6 Halted 0 ms 0 KB -