[UOJ#245][UER#7]天路(近似算法)

时间:2021-08-12 23:52:54

允许5%的相对误差,意味着我们可以只输出$\log_{1.05} V$种取值并保证答案合法。并且注意到答案随着区间长度而单增,故取值不同的答案区间是$O(\log_{1.05} V)$的。

于是初始x=0,每次x=max(x+1,x*1.05),再用单调队列$O(n)$找出当前x能更新的区间即可。总复杂度$O(n\log_{1.05}V)$

 #include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int n,h1,h2,r1,r2,a[N],q1[N],q2[N],x,l=,r; int main(){
scanf("%d",&n);
rep(i,,n) scanf("%d",&a[i]);
for(; l<=n; x=max(x+,int(x*1.05))){
h1=h2=; r=r1=r2=; int now=;
rep(i,,n){
while(r1>=h1 && a[q1[r1]]<a[i]) r1--;
while(r2>=h2 && a[q2[r2]]>a[i]) r2--;
q2[++r2]=q1[++r1]=i;
for(; a[q1[h1]]-a[q2[h2]]>x; now++){
if(q1[h1]==now)h1++;
if(q2[h2]==now)h2++;
}
r=max(r,i-now+);
}
for(; l<=r; l++)printf("%d\n",x);
}
return ;
}