Frog
题解:模拟肯定是不能模拟的了,就考虑怎样放石头才能让青蛙跳的次数尽量多了。
能想到的只有在
L
+
1
L+1
L+1的长度内最多跳两次,所以我们就去尽可能构造
L
+
1
L+1
L+1。如果当前石头距离上一次石头的距离大于
L
L
L,那么就需要单独再跳一次,否则就可以归到上一次跳跃。
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,L,a[200100];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
ios::sync_with_stdio(false);
int T, w = 0;
cin>>T;
while(T--){
cin>>n>>m>>L;
for(int i = 1; i <= n; ++i){
cin>>a[i];
}
a[n + 1] = m;
sort(a + 1,a + n + 1);
int cnt = 0, pre = -L;
for(int i = 1; i <= n + 1; ++i){
cnt += (a[i] - a[i-1]) / (L + 1) * 2;
pre += (a[i] - a[i-1]) / (L + 1) * (L + 1);
if(a[i] - pre > L){ //剩余的大于L就需要单独再跳一次,否则就可以归为上一步
pre = a[i-1] + (a[i] - a[i-1]) / (L + 1) * (L + 1);
cnt++;
}
}
printf("Case #%d: %d\n",++w,cnt);
}
return 0;
}