greedy simulation
Let's start with this example
6
1 2 3 4 5 6
4 2 3 5 3 1
If current X = 1 X = 1X=1 , then we can build aFFF array
F [ i ] F[i]F [ i ] means to go in and out from the entrance, and add the firsta [ i ] a[i]a [ i ] Home users promote sales, do not take redundant roads, and will contribute to the answer.
Useans ansan s means the current answer,
usenow nown o w represents the farthest point
Then X = 1 X = 1X=1 时: F [ i ] = s [ i ] ∗ 2 + a [ i ] F[i] = s[i]*2+a[i] F[i]=s[i]∗2+a[i]
F = F = F= {
6 , 6 , 9 , 13 , 13 , 13 6, 6, 9, 13, 13, 13 6,6,9,13,13,13 }
So we start fromF[i] F[i]Just pick a maximum value in F [ i ]
Setnow = max.id now = max.idnow=max.id 即 n o w = 4 now = 4 now=4 (in fact, 5 or 6 is also acceptable)
ans ansans += F [ 4 ] F[4] F[4]
( a n s ans ans == 13 13 13 )
Then think about it, what will be the effect on otherF[i]F[i]What is the effect of F [ i ] ?
ConsideringX = 2 X = 2X=2 o'clock.
young s [ i ] s[i]s[i] < s [ n o w ] s[now] s [ n o w ] is ins [ now ] s[now]left side of s [ n o w ]
F [ i ] F[i] F[i] = a [ i ] a[i] a[i]
Because Keyi is going now nowSales on the road at point n o w
Supplement:Actually go to sales first iii point and go to sellnow nowThe fatigue value of point n o w is the same
, thenFFF becomes { 4 , 2 , 3 , / , 13 , 13 4, 2, 3, /, 13, 13
4,2,3,/,13,13}
young s [ i ] s[i]s[i] > s [ n o w ] s[now] s [ n o w ] is ins [ now ] s[now]Right side of s [ n o w ]
F [ i ] = ( s [ i ] − s [ n o w ] ) ∗ 2 + a [ i ] F[i] = (s[i]-s[now])*2+a[i] F[i]=(s[i]−s[now])∗2+a[i]
Because you can go two more distances forward on the original distance ( s [ i ] − s [ now ] s[i]-s[now]s[i]−s[now]) ∗ 2 *2 ∗ 2 (one trip, one trip)
soFFF becomes { 4 , 2 , 3 , / , 5 , 5 4, 2, 3, /, 5, 5
4,2,3,/,5,5}
Now, we start again from FFFind the ID of the maximum value in the F
array letnow = 6 now = 6now=6 ( 5 5 5 also works)
ans ansans += F[6]
( a n s ans ans == 18 18 18 )
At this time
FFF = {
4 , 2 , 3 , / , 3 , / 4, 2, 3, /, 3, / 4,2,3,/,3,/}
F [ 5 ] F[5] F [ 5 ] becomes3 33 , because it is in6 66
There are no pointson the leftFFF value
X = 3 X = 3 X=3 o'clock
ans ansans += F[1]
( a n s ans ans == 22 22 22)
F F F = {
/ , 2 , 3 , / , 3 , / /, 2, 3, /, 3, / /,2,3,/,3,/}
X = 4 X = 4 X=4 o'clock
ans ansans += F[3]
( a n s ans ans == 25 25 25)
F F F = {
/ , 2 , / , / , 3 , / /, 2, /, /, 3, / /,2,/,/,3,/}
X = 5 X = 5 X=5 o'clock
ans ansans += F[5]
( a n s ans ans == 28 28 28)
F F F = {
/ , 2 , / , / , / , / /, 2, /, /, /, / /,2,/,/,/,/}
X = 6 X = 6 X=6 o'clock
ans ansans += F[2]
( a n s ans ans == 30 30 30)
F F F = {
/ , / , / , / , / , / /, /, /, /, /, / /,/,/,/,/,/ }
The final answer is:
13
18
22
25
28
30
So here comes the question, how to quickly find the maximum value?
We use maxn [now] maxn[now]maxn[now] 表示 n o w now now ~ n n n 中 F [ i ] F[i] F[i] 的最大值 ( n o w < i < = n now < i <= n now<i<=n)
m a x n [ i ] maxn[i] The role of max x n [ i ] is to maintainthe now nowF [ i ] F[i]on the right side of n o wThe maximum value of F [ i ]
thennow nowThe largest a [ i ] a[i]on the left side of n o wHow to maintain a [ i ]
? Becausenow nowThe value on the left side of n o w needs to have the operation of deleting the maximum value
, somaxn maxnThe preprocessing operation of max x n
, but priority_queue is a good data structure,
you can use the priority queueqqq maintains the maximuma [ i ] a[i]the value of a [ i ]
You will find that these operations will not use FFF array, so there is no need to create this array, let alone change it,
now nowa [ i ] a[i]on the left side of n o wa [ i ] will not change, andthe maxn [ now ] maxn[now]max x n [ n o w ] only needs to subtracts [ now ] ∗ 2 s[now]*2s[now]∗2 即可
m a x n [ n o w ] maxn[now] maxn[now] = s [ i ] ∗ 2 + a [ i ] s[i]*2+a[i] s[i]∗2+a [ i ] , ( i$ is the maximum value at this time)
somaxn [ now ] − s [ now ] ∗ 2 maxn[now]-s[now]*2maxn[now]−s[now]∗2 = a [ i ] + s [ i ] ∗ 2 − s [ n o w ] ∗ 2 a[i]+s[i]*2-s[now]*2 a[i]+s[i]∗2−s[now]∗2 = a [ i ] + ( s [ i ] − s [ n o w ] ) ∗ 2 a[i]+(s[i]-s[now])*2 a[i]+(s[i]−s[now])∗2 is the aboveF [ i ] F[i]value of F [ i ]
Ac Code:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,INF=1e9,mod=INF+7;
int n,now,cnt;
long long ans;
int a[N],s[N];
pair<int,int> maxn[N];
priority_queue<int> q;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d",s+i);
for(int i = 1;i <= n;i++)
scanf("%d",a+i);
maxn[n+1].second = -INF;
for(int i = n;i >= 1;i--){
maxn[i] = maxn[i+1];
if(s[i]*2+a[i] >= maxn[i].second)
maxn[i] = make_pair(i,s[i]*2+a[i]);
}
for(int i = 1;i <= n;i++){
int l = (q.empty() ? -INF : q.top());
int r = (now == n ? -INF : maxn[now+1].second);
if(l >= r-s[now]*2){
ans += l;
q.pop();
printf("%lld\n",ans);
}else {
ans += r-s[now]*2;
for(int i = now+1;i <= maxn[now+1].first-1;i++)
q.push(a[i]);
now = maxn[now+1].first;
printf("%lld\n",ans);
}
}
return 0;
}