PS 부수기
백준 11001 : 김치 본문
$maxdate[s]=$ $s$일날 김치를 넣었을 때 최대의 김치싸대기를 날릴 수 있는, 김치를 빼는 날짜 $e$
$res(s,e)=$ $s$일날 김치를 넣고, $e$일날 김치를 뺐을 때 날리는 김치싸대기의 힘
그러면 정의에 따라
$res(s,e) = (e-s) \times t[e] + b[s]$이고,
$s$를 고정시킨다면 가능한 모든 $res(s,e)$값 중 $res(s,maxdate[s])$가 제일 클 것이다.
우리는 각 $s$ 시작일 마다 $res(s,maxdate[s])$값을 찾아주고 그들 중 최댓값을 구하면 된다.
그 전에, 우리는 다음 식을 증명할 수 있다.
$maxdate[s] \leq maxdate[s+1]$
즉, 김치를 나중에 넣는다면, 최적의 빼는 날 $maxdate$가 김치를 일찍 넣었을 때보다 무조건 같거나 늦는다는 것이다.
증명:
$res(s,e) = (e - s) \times t[e] + b[s]$이며, $e$가 $maxdate[s]$일 때 최댓값을 가진다.
$res(s+1,e) = (e-s-1) \times t[e] + b[s+1]$
$=(e-s) \times t[e] + b[s] - b[s] -t[e] + b[s+1]$
$=res(s,e) - t[e] -b[s] + b[s+1]$
$res(s,e)$값은 $e$가 $maxdate[s]$일 때 최댓값을 가지므로
$res(s,e) - t[e] -b[s] + b[s+1]$를 최대화 시켜주는 $e$값을 구하려면 $t[e]$값이 $t[maxdate[s]]$보다 커야한다.
$t$배열은 감소하는 배열이므로 $e \geq maxdate[s]$를 만족해야 최댓값을 노려볼 수라도 있다.
그렇다면 어떤 $s$에 대해서 $maxdate[s]$를 구했다면,
$s$보다 큰 날짜들에서는 $maxdate[s]$ 이상인 날짜에서만 끝점을 탐색하면 되고,
반대로 $s$보다 작은 날짜들에서는 $maxdate[s]$ 이하인 날짜에서만 끝점을 탐색하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define LLINF 0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
#define uniq(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define sz(x) (int)x.size()
#define pw(x) (1LL<<x)
using pii = pair<int, int>;
using ll = long long;
const ll MOD = 1e9 + 7;
const long double PI = acos(-1.0);
const int N = 100100;
int n, d;
ll t[N], b[N], dp[N];
ll ans = 0;
void solve(int l, int r, int s, int e) {
// 탐색 범위 - s ~ e
// cur ~ x의 값이 제일 큰 x를 찾자
if(l > r) return;
int cur = (l + r) / 2;
ll mx = 0;
int mxidx = -1;
for(int i=s ; i<=e ; i++) {
if(i < cur) continue;
if(cur + d < i) continue;
ll curans = (i - cur) * t[i] + b[cur];
if(mx < curans) {
mxidx = i;
mx = curans;
}
}
ans=max(ans,mx);
if(l == r) return;
solve(l, cur - 1, s, mxidx);
solve(cur + 1, r, mxidx, e);
}
int main() {
IOS;
cin >> n >> d;
for(int i=0 ; i<n ; i++)
cin >> t[i];
for(int i=0 ; i<n ; i++)
cin >> b[i];
solve(0,n-1,0,n-1);
cout << ans << '\n';
}
|
cs |
'PS' 카테고리의 다른 글
백준 12858 : Range GCD (1) | 2021.09.17 |
---|---|
세그멘트트리 많이 보는 유형 3문제 (0) | 2021.09.14 |
2022 KAKAO BLIND RECRUITMENT 코테 후기 (0) | 2021.09.11 |
백준 19851 - 버거운 버거 (0) | 2021.09.08 |
백준 20681 - Black Family Tree (0) | 2021.02.05 |