这两道都是斜率优化的题目,完全一样的思路,双倍经验当然要收(就是读入坑了我好几次)。
设
f[i]
f
[
i
]
为在
i
i
位置建设了仓库的最小代价,则有:
前缀和优化一下,将原式转为:
f[i]=min(f[j]+x[i]×(s2[i]−s2[j])−(s1[i]−s1[j]))+c[i](0≤j<i)
f
[
i
]
=
min
(
f
[
j
]
+
x
[
i
]
×
(
s
2
[
i
]
−
s
2
[
j
]
)
−
(
s
1
[
i
]
−
s
1
[
j
]
)
)
+
c
[
i
]
(
0
≤
j
<
i
)
最后得:
(f[i]+s1[j])−(f[k]+s1[k])s2[j]−s2[k]<x[i]
(
f
[
i
]
+
s
1
[
j
]
)
−
(
f
[
k
]
+
s
1
[
k
]
)
s
2
[
j
]
−
s
2
[
k
]
<
x
[
i
]
上代码:
#include<bits/stdc++.h>
#define il inline
#define int long long
using namespace std;
const int N=1000005;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=(x+(x<<2)<<1)+c-48;
return x*f;
}
int n,h,t,x[N],p[N],c[N],f[N],s1[N],s2[N],q[N];
il int Y(int i){return f[i]+s1[i];}
il int X(int i){return s2[i];}
il double slope(int j,int k){return 1.0*(Y(j)-Y(k))/(X(j)-X(k));}
signed main(){
n=read();
for(int i=1;i<=n;++i)
x[i]=read(),p[i]=read(),c[i]=read(),s2[i]=s2[i-1]+p[i],s1[i]=s1[i-1]+x[i]*p[i];
for(int i=1;i<=n;++i){
while(h<t&&slope(q[h+1],q[h])<x[i]) ++h;
int j=q[h];
f[i]=f[j]+x[i]*(s2[i]-s2[j])-s1[i]+s1[j]+c[i];
while(h<t&&slope(q[t],q[t-1])>slope(i,q[t])) --t;
q[++t]=i;
}
return !printf("%lld",f[n]);
}
而小P的牧场就可以使 x[i]=i x [ i ] = i ,然后注意一下输入(其实没什么坑),就一样了。
#include<bits/stdc++.h>
#define il inline
#define int long long
using namespace std;
const int N=1000005;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=(x+(x<<2)<<1)+c-48;
return x*f;
}
int n,h,t,p[N],c[N],f[N],s1[N],s2[N],q[N];
il int Y(int i){return f[i]+s1[i];}
il int X(int i){return s2[i];}
il double slope(int j,int k){return 1.0*(Y(j)-Y(k))/(X(j)-X(k));}
signed main(){
n=read();
for(int i=1;i<=n;++i) c[i]=read();
for(int i=1;i<=n;++i) p[i]=read(),s2[i]=s2[i-1]+p[i],s1[i]=s1[i-1]+i*p[i];
for(int i=1;i<=n;++i){
while(h<t&&slope(q[h+1],q[h])<i) ++h;
int j=q[h];
f[i]=f[j]+i*(s2[i]-s2[j])-s1[i]+s1[j]+c[i];
while(h<t&&slope(q[t],q[t-1])>slope(i,q[t])) --t;
q[++t]=i;
}
return !printf("%lld",f[n]);
}
刚又发现防御准备这道题又是一样的,博客前面的就不改了,其实是三倍经验啊,只要把 x[i] x [ i ] 改为 i i ,把改为 1 1 <script type="math/tex" id="MathJax-Element-22">1</script>就好。
#include<bits/stdc++.h>
#define il inline
#define int long long
using namespace std;
const int N=1000005;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=(x+(x<<2)<<1)+c-48;
return x*f;
}
int n,h,t,c[N],f[N],s1[N],s2[N],q[N];
il int Y(int i){return f[i]+s1[i];}
il int X(int i){return s2[i];}
il double slope(int j,int k){return 1.0*(Y(j)-Y(k))/(X(j)-X(k));}
signed main(){
n=read();
for(int i=1;i<=n;++i)
c[i]=read(),s2[i]=s2[i-1]+1,s1[i]=s1[i-1]+i*1;
for(int i=1;i<=n;++i){
while(h<t&&slope(q[h+1],q[h])<i) ++h;
int j=q[h];
f[i]=f[j]+i*(s2[i]-s2[j])-s1[i]+s1[j]+c[i];
while(h<t&&slope(q[t],q[t-1])>slope(i,q[t])) --t;
q[++t]=i;
}
return !printf("%lld",f[n]);
}