※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 5254번 문제인 Balls이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/5254 

 

5254번: Balls

There is one standard coordinate system with n balls and n holes in it: balls are located at positions (1, h), (2, h), …, (n, h), for some h > 0, and holes are located at (1, 0), (2, 0), …, (n, 0). At a given moment, all balls start falling down (along

www.acmicpc.net

left obstacle이 설치된 경우의 풀이를 생각해보자. right obstacle이 설치된 경우의 풀이는 배열의 순서를 뒤집어 똑같이 풀 수 있으므로 이 경우의 풀이만 생각해도 충분하다.

 

An을 1 이상 n 미만인 정수 k에 대하여 [k,n]의 위치에 left obstacle을 설치했을 때 얻을 수 있는 최대 점수로 정의하자. nk가 고정됐을 때 얻게 되는 점수는 다음 식과 같이 나타난다. (Sn[1,n]에 속한 ck값들의 합을 의미한다.)

Sk+ck(nk)+SNSn

 

따라서 An은 다음과 같이 쓸 수 있다.

An=max(Sk+ck(nk)+SNSn) (k<N)

 

이 식을 변형해 다음과 같이 쓰면 max 안의 식은 n에 대한 일차식이 됨을 관찰할 수 있다.

An=max((ck)n+(Skkck))+(SNSn) (k<N)

 

위의 식을 이용해, 각 An의 값을 앞서 나온 k값에 대응되는 일차식의 값중 최댓값을 반복해 구하는 것으로 문제를 해결할 수 있다. 이는 Li-Chao Tree를 이용해 해낼 수 있다.

 

정확히 하나의 obstacle을 설치해야 한다는 조건에 유의하자. 즉, obstacle을 전혀 설치하지 않는 경우는 문제의 답이 될 수 없다는 점에 유의하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

struct node {
	ll a = 0, b = -1000000000000000007LL;
	int l = -1, r = -1;
	node() {};
};

ll N;
int idx = 1;
node nodes[300001];

void update(ll L, ll R, ll a, ll b, int sI) {
	node& cur = nodes[sI];
	ll LU = a * L + b, LN = cur.a * L + cur.b;
	ll RU = a * R + b, RN = cur.a * R + cur.b;
	if (LU >= LN && RU >= RN) {
		cur.a = a, cur.b = b;
		return;
	}
	if (LU <= LN && RU <= RN) return;

	ll M = (L + R) / 2;
	ll MU1 = a * M + b, MN1 = cur.a * M + cur.b;
	ll MU2 = a * M + a + b, MN2 = cur.a * M + cur.a + cur.b;
	if (LU >= LN && MU1 >= MN1) {
		if (cur.r < 0) {
			nodes[idx] = node();
			cur.r = idx++;
		}
		update((L + R) / 2 + 1, R, cur.a, cur.b, cur.r);
		cur.a = a, cur.b = b;
	}
	else if (LU <= LN && MU1 <= MN1) {
		if (cur.r < 0) {
			nodes[idx] = node();
			cur.r = idx++;
		}
		update((L + R) / 2 + 1, R, a, b, cur.r);
	}
	else if (MU2 >= MN2 && RU >= RN) {
		if (cur.l < 0) {
			nodes[idx] = node();
			cur.l = idx++;
		}
		update(L, (L + R) / 2, cur.a, cur.b, cur.l);
		cur.a = a, cur.b = b;
	}
	else {
		if (cur.l < 0) {
			nodes[idx] = node();
			cur.l = idx++;
		}
		update(L, (L + R) / 2, a, b, cur.l);
	}
}

ll qry(ll L, ll R, ll x) {
	int sI = 0;
	ll ret = -1000000000000000007LL;
	while (sI > -1) {
		ret = max(ret, nodes[sI].a * x + nodes[sI].b);
		ll M = (L + R) / 2;
		if (x <= M) R = M, sI = nodes[sI].l;
		else L = M + 1, sI = nodes[sI].r;
	}

	return ret;
}

ll psum, total;
ll arr[300001];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;

	for (ll n = 1; n <= N; n++) {
		ll& cur = arr[n]; cin >> cur;
		total += cur;
	}

	nodes[0] = node(), psum = 0;
	ll ans1 = -1000000000000000000LL;
	for (ll n = 1; n <= N; n++) {
		psum += arr[n];
		ans1 = max(ans1, qry(-1000000000, 1000000000, n) + total - psum);
		update(-1000000000, 1000000000, arr[n], psum - n * arr[n], 0);
	}

	nodes[0] = node(), psum = 0;
	ll ans2 = -1000000000000000000LL;
	for (ll n = 1; n <= N; n++) {
		psum += arr[N - n + 1];
		ans2 = max(ans2, qry(-1000000000, 1000000000, n) + total - psum);
		update(-1000000000, 1000000000, arr[N - n + 1], psum - n * arr[N - n + 1], 0);
	}

	cout << ans2 << '\n' << ans1;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 13022 // C++] 늑대와 올바른 단어  (0) 2023.06.18
[BOJ 2878 // C++] 캔디캔디  (0) 2023.06.17
[BOJ 10067 // C++] 수열 나누기  (0) 2023.06.15
[BOJ 13230 // C++] Bits equalizer  (0) 2023.06.14
[BOJ 11334 // C++] Coin Turning Game  (0) 2023.06.13

+ Recent posts