※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 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이 설치된 경우의 풀이는 배열의 순서를 뒤집어 똑같이 풀 수 있으므로 이 경우의 풀이만 생각해도 충분하다.
따라서
이 식을 변형해 다음과 같이 쓰면 max 안의 식은
위의 식을 이용해, 각
정확히 하나의 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;
}
'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 |