「LAOI-21」砕月 题解
出题人题解。这题的子任务版本 T764425。
表述一:初始时令变量
t=a_l 。从l+1 到r 枚举下标i ,依次执行操作t:=\lfloor\frac{\max(t,a_i)}{\min(t,a_i)}\rfloor 。
表述二:初始时令变量t=1 。从l 到r 枚举下标i ,依次执行操作t:=\lfloor\frac{\max(t,a_i)}{\min(t,a_i)}\rfloor 。
不难证明这两个表述等价。若一个好的区间操作过程中(不包括初始和结束)未出现过
根据表述二,一个好的区间必然由多个极小好的区间拼接而成。因此尝试找出所有极小好的区间。
显然每个左端点
题目中给出了序列单调不增的性质,这意味着若极小好的区间若不为
再来考虑极小好的区间的拼接部分。尝试动态规划,设
综上,时间复杂度
#include<bits/stdc++.h>
#include<ext/rope>
using namespace __gnu_cxx;
#define pb push_back
#define fi first
#define se second
#define dbg puts("-------------qaqaqaqaqaqaqaqaqaq------------")
#define pl (p<<1)
#define pr ((p<<1)|1)
#define pii pair<int,int>
#define int long long
#define ull unsigned long long
#define ent putchar('\n')
#define sp putchar(' ')
using namespace std;
const int mod=998244353,inf=INT_MAX/4;
const double eps=1e-9;
inline int read(){
char c=getchar(),f=0;int t=0;
for(;c<'0'||c>'9';c=getchar()) if(!(c^45)) f=1;
for(;c>='0'&&c<='9';c=getchar()) t=(t<<1)+(t<<3)+(c^48);
return f?-t:t;
}
inline void write(int x){
static int t[35];int tp=0;
if(x==0) return(void)(putchar('0'));else if(x<0) putchar('-'),x=-x;
while(x) t[tp++]=x%10,x/=10;
while(tp--) putchar(t[tp]+48);
}
const int N=1e6+10,RN=1e6;
int T,n,ans;
int a[N],b[N],dp[N];
signed main(){
T=read();
while(T--){
n=read(),ans=0;
for(int i=1;i<=n;i++) a[i]=read(),dp[i]=b[i]=0;
for(int i=1;i<=n;i++){
for(int j=i,cur=1;j<=n;j++){
int t1=max(cur,a[j]),t2=min(cur,a[j]);
cur=t1/t2;
if(cur==1){b[i]=j;break;}
}
}
for(int i=1;i<=n;i++){
if(b[i]) dp[b[i]]+=dp[i-1]+1;
ans+=dp[i];
}
write(ans),ent;
}
}