#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
const int mxn=1e5+7;
vector<ll> m;
vector<ll> b;
ll h[mxn];
ll w[mxn];
ll p[mxn];
ll dp[mxn];
bool bad(int f1,int f2,int f3)
{
return (b[f3]-b[f1])*(m[f1]-m[f2])<=(b[f2]-b[f1])*(m[f1]-m[f3]);
}
void add(ll m_,ll b_)
{
m.push_back(m_);
b.push_back(b_);
int sz=m.size();
while(sz>=3 && bad(sz-3,sz-2,sz-1))
{
m.erase(m.end()-2);
b.erase(b.end()-2);
sz--;
}
}
ll func(int p,ll x)
{
return m[p]*x+b[p];
}
ll query(ll x)
{
ll ans=1e18;
int lo=0;
int hi=m.size()-1;
while(lo<=hi)
{
int mid1=lo+(hi-lo)/3;
int mid2=hi-(hi-lo)/3;
if(func(mid1,x)<func(mid2,x))
{
ans=min(ans,func(mid1,x));
hi=mid2-1;
}
else
{
ans=min(ans,func(mid2,x));
lo=mid1+1;
}
}
return ans;
}
void solve(int l,int r)
{
if(l==r)return;
int mid=l+r>>1;
solve(l,mid);
vector<int> v;
for(int i=l;i<=mid;i++)v.push_back(i);
m.clear();
b.clear();
sort(v.begin(),v.end(),[](int a,int b)
{
if(h[a]==h[b])
{
return dp[a]+h[a]*h[a]-p[a]>dp[b]+h[b]*h[b]-p[b];
}
return h[a]<h[b];
});
for(int i=0;i<v.size();i++)
{
int k=v[i];
add(-2*h[k],dp[k]+h[k]*h[k]-p[k]);
}
for(int i=mid+1;i<=r;i++)
{
dp[i]=min(dp[i],query(h[i])+h[i]*h[i]+p[i-1]);
}
solve(mid+1,r);
}
int main()
{
for(int i=1;i<mxn;i++)dp[i]=1e18;
dp[1]=0;
int n;
cin>>n;
for(int i=1;i<=n;i++)scanf("%lld",&h[i]);
p[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&w[i]);
p[i]=p[i-1]+w[i];
}
solve(1,n);
cout<<dp[n]<<endl;
return 0;
}
Compilation message
building.cpp: In function 'void solve(int, int)':
building.cpp:62:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=l+r>>1;
~^~
building.cpp:77:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++)
~^~~~~~~~~
building.cpp: In function 'int main()':
building.cpp:102:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++)scanf("%lld",&h[i]);
~~~~~^~~~~~~~~~~~~~
building.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&w[i]);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
6 ms |
1152 KB |
Output is correct |
5 |
Correct |
6 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
4332 KB |
Output is correct |
2 |
Correct |
188 ms |
4388 KB |
Output is correct |
3 |
Correct |
193 ms |
4464 KB |
Output is correct |
4 |
Correct |
179 ms |
4452 KB |
Output is correct |
5 |
Correct |
119 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
6 ms |
1152 KB |
Output is correct |
5 |
Correct |
6 ms |
1152 KB |
Output is correct |
6 |
Correct |
198 ms |
4332 KB |
Output is correct |
7 |
Correct |
188 ms |
4388 KB |
Output is correct |
8 |
Correct |
193 ms |
4464 KB |
Output is correct |
9 |
Correct |
179 ms |
4452 KB |
Output is correct |
10 |
Correct |
119 ms |
4856 KB |
Output is correct |
11 |
Correct |
184 ms |
4372 KB |
Output is correct |
12 |
Correct |
189 ms |
4432 KB |
Output is correct |
13 |
Correct |
148 ms |
4372 KB |
Output is correct |
14 |
Correct |
189 ms |
4396 KB |
Output is correct |
15 |
Correct |
115 ms |
5224 KB |
Output is correct |
16 |
Correct |
118 ms |
4856 KB |
Output is correct |
17 |
Correct |
81 ms |
4448 KB |
Output is correct |
18 |
Correct |
89 ms |
4420 KB |
Output is correct |