UVA 1372 - Log Jumping(推理)

题目链接:1372 - Log Jumping

题意:给定一些n个木板的起始位置和长度k,相重叠的木板可以互相跳跃,求能构成环的最大数量。
思路:先按起始位置排序,然后每次多一个木板就去判断他和前一个和前前一个能不能互相跳跃,如果可以的话就可以多加上这个木板。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 5005;
int t, n, k, i, start[N];

bool check(int s, int e) {
	return e - s <= k;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		int ans = 0;
		scanf("%d%d", &n, &k);
		for (i = 0; i< n; i++)
			scanf("%d", &start[i]);
		sort(start, start + n);
		i = 0;
		while (i < n) {
			int sum = 1;
			while (i < n - 1 && ((sum == 1 && check(start[i], start[i + 1])) || 
				(check(start[i], start[i + 1]) && check(start[i - 1], start[i + 1]))))
				sum++, i++;
			if (!check(start[i], start[i + 1]) || i == n - 1) i++;
			ans = max(ans, sum);
		}
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值