HDU 5037 Frog(北京网络赛F题)

HDU Frog

题目链接

思路:贪心搞,记录青蛙当前和上一次的位置,如果遇到一个区间跳不过去,要加石头,就按两步长度为(l + 1)去放会使得步数最大,注意边界条件有可能一步就能跳过去

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 200005;

int t, n, m, l;
int r[N];

int solve() {
	int ans = 0;
	int now = 0;
	int pre = -l;
	int st = 0;
	while (now < m) {
		if (now + l < r[st]) {
			int tmp = now - pre;
			int len = r[st] - now;
			int ci = len / (l + 1) - 1;
			ans += ci * 2;
			now += ci * (l + 1);
			pre = now - tmp;
			ans++;
			pre = now;
			now += (1 + l) - tmp;
		} else {
			while (now + l >= r[st] && st <= n)
				st++;
			pre = now;
			now = r[st - 1];
			ans++;
		}
	}
	return ans;
}

int main() {
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d", &n, &m, &l);
		for (int i = 0; i < n; i++)
			scanf("%d", &r[i]);
		sort(r, r + n);
		r[n] = m;
		printf("Case #%d: %d\n", ++cas, solve());
	}
	return 0;
}


你可能感兴趣的:(HDU 5037 Frog(北京网络赛F题))