纯爆搜16分求条,没TLE
查看原帖
纯爆搜16分求条,没TLE
923248
Lian_zy楼主2024/7/1 11:55

rt,思路是枚举一个长度为2n−12^{n-1}的二进制串,如果第ii位为11表示ii和i+1i+1分割开

然后枚举完判断就行了,结果不对

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

const int maxn = 20;
long long ans;
int n, w, pos, cnt = maxn, c[maxn];
int countbit(int x) {
	if (x) return countbit(x & (x - 1)) + 1;
	return 0;
}
int main() {
	scanf("%d %d", &n, &w);
	for (int i = 1; i <= n; i++) scanf("%d", c + i);
	for (int s = 0; s < (1 << n - 1); s++) {
		pos = 1;
		ans = c[1];
		for (int i = 1; i < n; i++) {
			if ((1 << i - 1) & s) {
				if (ans > w) break;
				pos = i;
				ans = 0;
			} 
			ans += c[i + 1];
		}
		if (ans <= w) cnt = min(cnt, countbit(s));
	}
	printf("%d\n", cnt + 1);
	return 0;
}
2024/7/1 11:55
加载中...