思路见代码
AC代码:
#include <bits/stdc++.h>
#define IN \
ios::sync_with_stdio(0); \
cin.tie(0);
using namespace std;
#define ll long long
const int N = 1e2 + 10, M = 1e4 + 10;
int n, m, ans;
int a[N];
int h = 10000;
bool f[N][M * 2]; // f[i][j]表示前i个数能否形成j
bool g[N][M * 2]; // g[i][j]表示前i个数形成j时,第i个数的正负
bool st[N];
bool volid(int x) {
return x >= -10000 && x <= 10000;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
f[2][a[1] - a[2] + h] = true;
for (int i = 2; i <= n; i++) {
for (int j = -10000; j <= 10000; j++) {
if (f[i][j + h]) { // 如果前i个数能形成j
if (volid(a[i + 1] + j)) { // 情况1:第i+1个数为正
f[i + 1][a[i + 1] + j + h] = true;
g[i + 1][a[i + 1] + j + h] = true;
}
if (volid(j - a[i + 1])) { // 情况2:第i+1个数为负
f[i + 1][j - a[i + 1] + h] = true;
g[i + 1][j - a[i + 1] + h] = false;
}
}
}
}
int id = n;
while (id > 2) { // 确定所有数的正负
st[id] = g[id][m + h];
if (st[id]) {
m -= a[id];
} else {
m += a[id];
}
id--;
}
st[1] = true;
st[2] = false;
// 正数对它前面的数进行cut操作,随后一直cut(1)
int cnt = 0;
for (int i = 3; i <= n; i++) {
if (st[i] == true) {
cout << i - 1 - cnt << endl;
cnt++;
}
}
for (int i = 1; i <= n - 1 - cnt; i++) { // 一共为n-1次操作
cout << "1" << endl;
}
return 0;
}