# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
92986 | emil_physmath | Bootfall (IZhO17_bootfall) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=30;
const int MAXSUM=30*20;
int a[MAXN];
vector<int> sums[MAXN];
vector<int> AllPossSums(int arr[], int n);
int main()
{
int n, sum=0;
cin>>n;
for (int i=0; i<n; i++)
cin>>a[i];
for (int i=0; i<n; i++)
{
sum+=a[i];
swap(a[i], a[n-1]);
sums[i]=AllPossSums(a, n-1);
swap(a[i], a[n-1]);
}
vector<int> temp=AllPossSums(a, n);
if (sum%2 || *lower_bound(temp.begin(), temp.end(), sum/2)!=sum/2)
{
cout<<"0\n";
return 0;
}
vector<int> ans;
for (int x=1; x<=100; x++)
{
bool isAns=true;
for (int i=0; i<n; i++)
{
if ((sum-a[i]+x)%2) {
isAns=false;
break;
}
auto it=lower_bound(sums[i].begin(), sums[i].end(), (sum-a[i]+x)/2-x);
if (*it!=(sum-a[i]+x)/2-x) {
isAns=false;
break;
}
}
if (isAns)
ans.push_back(x);
}
cout<<ans.size()<<'\n';
for (int i=0; i<ans.size(); i++)
cout<<ans[i]<<' ';
cout<<'\n';
char I;
cin >> I;
return 0;
}
bool dp[MAXN][MAXSUM+1];
vector<int> AllPossSums(int arr[], int n)
{
int sum=0;
for (int i=0; i<n; i++)
sum+=arr[i];
memset(dp, 0, sizeof(dp));
for (int i=0; i<=n; i++)
dp[i][0]=true;
for (int i=1; i<=n; i++)
{
dp[i][arr[i-1]]=true;
for (int j=1; j<=sum; j++)
{
if (dp[i-1][j]==true)
{
dp[i][j]=true;
dp[i][j+arr[i-1]]=true;
}
}
}
vector<int> res;
for (int j=0; j<=sum; j++)
if (dp[n][j]==true)
res.push_back(j);
return res;
}