이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define y1 theboatman
#define make_struct(args...) {args}
using namespace std;
int main() {
cin.tie(0);
ios :: sync_with_stdio(0);
int n;
cin >> n;
int sum = 0;
vector <int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
vector <vector <int> > dp_pref(n + 1, vector <int> (sum * 2 + 1));
dp_pref[0][sum] = 1;
vector <set <int> > pref(n + 1);
pref[0].insert(sum);
for(int i = 1; i <= n; i++) {
for(int bal = 0; bal <= sum * 2; bal++) {
if (dp_pref[i - 1][bal]) {
dp_pref[i][bal + a[i]] = 1;
dp_pref[i][bal - a[i]] = 1;
}
}
for(int bal = 0; bal <= sum * 2; bal++) {
if (dp_pref[i][bal]) {
pref[i].insert(bal);
}
}
}
vector <vector <int> > dp_suff(n + 2, vector <int> (sum * 2 + 1));
dp_suff[n + 1][sum] = 1;
vector <set <int> > suff(n + 2);
suff[n + 1].insert(sum);
for(int i = n; i > 0; i--) {
for(int bal = 0; bal <= sum * 2; bal++) {
if (dp_suff[i + 1][bal]) {
dp_suff[i][bal + a[i]] = 1;
dp_suff[i][bal - a[i]] = 1;
}
}
for(int bal = 0; bal <= sum * 2; bal++) {
if (dp_suff[i][bal]) {
suff[i].insert(bal);
}
}
}
vector <int> ans;
for(int power = 1; power <= sum; power++) {
int ok = 0;
if (!dp_pref[n][sum]) {
ok = -1;
}
for(int i = 1; i <= n; i++) {
if (ok != i - 1) {
break;
}
for(auto it : pref[i - 1]) {
//cout << it << endl;
if (suff[i + 1].find(2 * sum - it - power) != suff[i + 1].end()) {
ok++;
break;
}
}
if (ok != i) {
break;
}
}
if (ok == n) {
ans.push_back(power);
}
}
cout << ans.size() << "\n";
for(auto i : ans) {
cout << i << " ";
}
return 0;
}
/*
x + power + y - sum - sum == 0
y == sum - x - power
1001
999
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |