[前缀和][dfs] Jzoj P5797 跳跃
题解
- 这题可以用dfs过,数据比较水,200+ms,非常优秀
- 记录个前缀和,剪枝一下
代码
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 int n; 6 long long ans,k,h[50],g[50],p[50],f[50],q[50]; 7 void dfs(int d,int last,long long sum) 8 { 9 if (d>n) 10 { 11 if (sum>=k) ans++; 12 return; 13 } 14 if (h[d]>=h[last]&&sum+g[d]>=k) ans+=f[d]; 15 else 16 if (h[d]>=h[last]&&sum+q[d]>=k) dfs(d+1,d,sum+g[d]); 17 if (sum+p[d+1]>=k) dfs(d+1,last,sum); 18 } 19 int main() 20 { 21 freopen("san.in","r",stdin); 22 freopen("san.out","w",stdout); 23 scanf("%d%lld",&n,&k); 24 for (int i=1;i<=n;i++) scanf("%lld%lld",&h[i],&g[i]); 25 f[n]=1,q[n]=g[n],p[n]=g[n],p[n+1]=0; 26 for (int i=n-1;i>0;i--) 27 { 28 f[i]=1,q[i]=0,p[i]=p[i+1]+g[i]; 29 for(int j=i+1;j<=n;j++) 30 if (h[j]>=h[i]) 31 { 32 f[i]+=f[j]; 33 if (q[j]>q[i]) q[i]=q[j]; 34 } 35 q[i]+=g[i]; 36 } 37 dfs(1,0,0); 38 printf("%lld",ans); 39 return 0; 40 }