#6为什么TLE!!!

P1929 迷之阶梯

**36行代码** nh-jump/2>=0 中,jump/2 自动下取整,实测出错,两边同乘2,改为 2*nh-jump>=0 [见AC记录](https://www.luogu.com.cn/record/40860514) 下面贴上代码 ```cpp #include<bits/stdc++.h> #define maxn 210 using namespace std; inline void read(int &x) { int y=1; x=0; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')y=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } x=x*y; } int n; int h[maxn]; int opt[maxn]; int main() { read(n); for(int i=1;i<=n;i++)read(h[i]); memset(opt,0x3f,sizeof(opt)); opt[1]=0; for(int i=2;i<=n;i++) { int nh=h[i],jump=1,j=i-1; for(int k=0;2*nh-jump>=0;k++,jump*=2) { for(;j-k>0;--j) { if(h[j-k]+jump<nh)break; opt[i]=min(opt[i],opt[j]+k+1); } } } if(opt[n]==opt[0])printf("-1\n");//写为 opt[n]=0x3f3f3f3f时第一个点WE,原因未知 else printf("%d\n",opt[n]); return 0; } ```
by Agir @ 2020-10-30 22:56:36


|