POJ1167 The Buses DFS

该博客分析了POJ1167问题,需要将给定序列分成等差数列,满足特定条件。首先预处理合法等差数列并按项数排序,然后使用DFS搜索,限制最多挑17个数列,并在过程中进行剪枝和合法性检查。
摘要由CSDN通过智能技术生成

题目链接

http://poj.org/problem?id=1167

分析

题意大概是将给定序列分成若干等差数列,使得每个数列满足 a 1 − d &lt; 0 , a n + d &gt; 59 a_1 - d &lt; 0, a_n + d &gt;59 a1d<0,an+d>59,且至少有两项,允许相同数列。

先预处理出哪些等差数列在一开始的局面是合法的,按照其项数排序以剪枝。

搜索每次挑出的数列,最多挑出 17 17 17 个,如果剩余数的个数处以当前数列加上已挑出的数列个数超过已有答案,剪枝;

因为已经挑出去了一些数,所以数列后来可能不合法,还需要再次检验。

AC代码

#include <cstdio>
#include <algorithm>

using namespace std;

inline int read() {
	int num = 0;
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9')
		num = num * 10 + c - '0', c = getchar();
	return num;
}

const int maxt = 65, maxr = 1350;

struct Route {
	int a, b, c;

	Route(int a = -1, int b = -1, int c = -1) : a(a), b(b), c(c) {}

	bool operator < (const Route& rhs) const {
		return c > rhs.c;
	}
} r[maxr];

int n, cnt[maxt], tot, ans = 17;

inline int judge(int a, int b) {
	for (int i = a; i < 60; i += b)
		if (!cnt[i]) return 0;
	return 1;
}

void dfs(int now, int find) {
	if (!n) {
		ans = min(ans, find);
		return;
	}
	for (int i = now; i <= tot; ++i) {
		if (find + n / r[i].c >= ans) return;
		if (judge(r[i].a, r[i].b)) {
			for (int j = r[i].a; j < 60; j += r[i].b) --n, --cnt[j];
			dfs(i, find + 1);
			for (int j = r[i].a; j < 60; j += r[i].b) ++n, ++cnt[j];
		}
	}
}


int main() {
	n = read();
	for (int i = 1; i <= n; ++i) ++cnt[read()];
	for (int i = 0; i <= 29; i++) if (cnt[i])
		for (int j = i + 1; i + j < 60; j++)
			if (judge(i, j)) r[++tot] = Route(i, j, (59 - i) / j + 1);
	sort(r + 1, r + tot + 1);
	dfs(1, 0);
	printf("%d", ans);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值