点击查看代码
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1100;
unordered_set<int> q;
int sum[N], ans[N], s[N], n;
bool flag;
void dfs(int i, int l, int r, int suml, int sumr)
{
if (l > r)//此时说明数组已经填完
{
int a = 0;
for (int i = 1; i <= n; i++)
a += ans[i];
if (a != sum[n * 2])
return;
flag = true;
}
if (flag)
return;
int qian = sum[i] - suml;//假设第i个是前缀
int hou = sum[i] - sumr;
if (q.find(qian) == q.end() && q.find(hou) == q.end())
return;
if (q.find(qian) != q.end())
{
ans[l] = qian;
dfs(i + 1, l + 1, r, suml + qian, sumr);
}
if (flag)
return;
if (q.find(hou) != q.end())
{
ans[r] = hou;
dfs(i + 1, l, r - 1, suml, sumr + hou);
}
}
int main()
{
int m;
cin >> n;
for (int i = 1; i <= n * 2; i++)
cin >> sum[i];
sort(sum + 1, sum + n * 2 + 1);
cin >> m;
while (m--)
{
int x;
cin >> x;
q.insert(x);
}
dfs(1, 1, n, 0, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
system("pause");
return 0;
}