rt,思路是枚举一个长度为2n−1的二进制串,如果第i位为1表示i和i+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;
}