weight

Topic links:

It is probably such a thing

According to the meaning of problems, we can know: this problem I will not , this problem needs to be sorted first number 2n

So that each of the (rear) and a small prefix most always queue or queue before the last

Set the number of k

It is judged that the total Sum (n) -k whether it can set in

Of course, if not among the collection, we will re back again

Until success

PS: If k both satisfied with the prefix and suffix and has satisfied, first on the front

because

Decentralization Code:

//weight
#include <bits/stdc++.h>

using namespace std;

const int N = 0x3f3f3f3f;
int n, s[N], m, a[N], t[N], flag, ans[N], all;

inline int read()
{
    int x = 0, y = 1;
    char z = getchar();
    while (z < '0' || z > '9')
    {
        if (z == '-')
        {
            y = -1;
            z = getchar();
        }
    }
    while (z >= '0' && x <= '9')
    {
        x = (x << 3) + (x << 1) + z - '0';
        z = getchar();
    }
}

inline void dfs(int l, int r, int now, int sum1, int sum2)
{
    if (flag)
        return;
    if (l == r)
    {
        int w3 = all - sum1 - sum2;
        if (t[w3])
        {
            ans[l] = w3;
            flag = 1;
        }
        return;
    }
    int w1 = s[now] - sum1;
    if (w1 <= 500 && w1 >= 1 && t[w1]) //属于前缀
    {
        ans[l] = w1;
        dfs(l + 1, r, now + 1, s[now], sum2);
    }
    if (flag)
        return;
    int w2 = s[now] - sum2;
    if (w2 <= 500 && w2 >= 1 && t[w2]) //属于后缀
    {
        ans[r] = w2;
        dfs(l, r - 1, now + 1, sum1, s[now]);
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= 2 * n; i++) //前后缀和
        scanf("%d", &s[i]);
    scanf("%d", &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &a[i]);
        t[a[i]] = 1;
    }
    sort(s + 1, s + 2 * n + 1);
    all = s[2 * n];
    dfs(1, n, 1, 0, 0);
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/gongcheng456/p/11007401.html