Question:
A clever enough frog wants to cross the river. It can jump at most at a time. The rice River is wide. m meters. There are n stones in the river. You can add any stone and ask the frog to jump several times at most.
Ideas:
It is obvious that the direction for testing ideas is not to confuse questions or greedy questions.
First of all, we clearly want to jump to the nearest possible number of times, but we cannot ignore that frogs are smart enough to think that we can jump to 1 + 1 meter every two steps.
Considering that there are some stones in the river, before each jump, you must first determine whether the stone can be jumped to the stone. If you can, you do not need to add a stone. Because the stone is near the frog, and you do not need to jump far from the frog, it does not match. maximum times
Finally, the most difficult point in this question is that the length of each step is limited by the length of the previous step. Therefore, you need to remember a last and skip one L + 1 at a time to satisfy the last limit. L + 1
Note:
The inputs to sort stones are not sequential.
If I is used to represent the sequence number of the stone at the front of the current position, maintain the sequence number after each hop. Otherwise, it may step on the stone.
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<cassert>#include<vector>#include<set>#include<map>#include<queue>using namespace std;#define N 200010int x[N];int T, t, n, m, L, ans;int main() {int i, now, last, time;scanf("%d", &T);for (t = 1; t <= T; t++) {scanf("%d%d%d", &n, &m, &L);for (i = 1; i <= n; i++)scanf("%d", &x[i]);n++;sort(x + 1, x + n);x[n] = m;now = ans = 0;last = L + 1;for (i = 1; now < m;) {if (now + L >= x[i]) {ans++;for (; i <= n; i++) {if (now + L < x[i])break;}i--;last = x[i] - now;now = x[i];i++;} else {time = (x[i] - now) / (L + 1) - 1;ans += time * 2;now += time * (L + 1);ans++;last = L + 1 - last;now += last;for (; i <= n; i++) {if (x[i] > now)break;}}}printf("Case #%d: %d\n", t, ans);}return 0;}
HDU 5037 frog