6-17 556 views
一只青蛙要过河,河里原来有n个石头,河的宽度为m,青蛙一次最多跳l长。在河里加一些石头帮助青蛙过河,同时青蛙想尽可能跳的次数少,你想让它尽可能跳的次数多。问最多跳几次。
思路:参考https://blog.csdn.net/qq_35781950/article/details/75207937
最优策略是跳两次前进l+1长度。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; /*尽可能的让青蛙多跳,。那就是如果那个青蛙 就是在他刚好跳过的第一个地方放石头,给他过 */ const int maxn=200006; int st[maxn]; int main() { int t; int m,n,l; scanf("%d",&t); for(int tt=1;tt<=t;tt++){ scanf("%d%d%d",&m,&n,&l); for(int i=1;i<=m;i++) scanf("%d",&st[i]); st[0]=0;st[++m]=n; sort(st,st+m+1); int k=l; int ans=0; for(int i=0;i<=m;i++) { int x=(st[i]-st[i-1])%(l+1); int y=(st[i]-st[i-1])/(1+l); if(k+x>l) { ans+=2*y+1; k=x; } else { ans+=2*y; k+=x; } } printf("Case #%d: %d\n",tt,ans); } return 0; }
Latest posts by 青鸟千秋 (see all)
- Ceph客户端缓存一致性分析 - 2019年6月21日
- CephFS:什么是CAPS? - 2019年6月21日
- Ceph集群快速搭建教程 - 2019年6月20日