Hướng dẫn giải của Chia vàng


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int n, a[24], p[13], ans;
pair <int,int> d[1000000], e[1000000];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    p[0] = 1;
    for (int i = 1; i <= 12; i++) p[i] = p[i - 1] * 3;

    int n1 = n / 2, n2 = n - n1;
    for (int i = 0; i < p[n1]; i++)
    {
        int s1 = 0, s2 = 0, tmp = i;
        for (int j = 0; j < n1; j++)
        {
            if (tmp % 3 == 1) s1 += a[j];
            if (tmp % 3 == 2) s2 += a[j];
            tmp /= 3;
        }
        d[i] = make_pair(s1 - s2, - s1 - s2);
    }

    for (int i = 0; i < p[n2]; i++)
    {
        int s1 = 0, s2 = 0, tmp = i;
        for (int j = 0; j < n2; j++)
        {
            if (tmp % 3 == 1) s1 += a[n1 + j];
            if (tmp % 3 == 2) s2 += a[n1 + j];
            tmp /= 3;
        }
        e[i] = make_pair(s1 - s2, - s1 - s2);
    }

    sort(d, d + p[n1]);
    int m1 = 1;
    for (int i = 1; i < p[n1]; i++)
        if (d[i].first != d[m1 - 1].first)
            d[m1++] = d[i];

    sort(e, e + p[n2]);
    int m2 = 1;
    for (int i = 1; i < p[n2]; i++)
        if (e[i].first != e[m2 - 1].first)
            e[m2++] = e[i];

    for (int i = 0, j = 0; i < m1 && j < m2;)
    {
        if (d[i].first == e[j].first) ans = max(ans, - d[i++].second - e[j++].second);
        else
            if (d[i].first < e[j].first) i++;
            else j++;
    }

    cout << ans / 2 << endl;
}

Code mẫu của happyboy99x

#include<bits/stdc++.h>
using namespace std;

#define TR(v,i) for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
const int N = 24, MAX = 531441;
int a[N], n;
pair<int, int> c1[MAX], c2[MAX];

void backtrack(int l, int r, pair<int, int> res[], int &c, int w1 = 0, int w2 = 0) {
    if(l == r) res[c++] = make_pair(w1 - w2, w1 + w2);
    else {
        backtrack(l+1, r, res, c, w1, w2);
        backtrack(l+1, r, res, c, w1 + a[l], w2);
        backtrack(l+1, r, res, c, w1, w2 + a[l]);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n; for(int i = 0; i < n; ++i) cin >> a[i];
    int n1 = 0, n2 = 0;
    backtrack(0, n/2, c1, n1); backtrack(n/2, n, c2, n2);
    sort(c1, c1+n1); sort(c2, c2+n2);
    int res = 0;
    for(int i = 0, j = n2-1; i < n1; ++i) {
        while(j > 0 && c1[i].first + c2[j].first > 0) --j;
        if(c1[i].first + c2[j].first == 0)
            res = max(res, c1[i].second + c2[j].second);
    }
    cout << res/2 << '\n';
    return 0;
}

Code mẫu của ladpro98

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define ii pair<int,int>
const int N = 25;
using namespace std;

pair<int, int> f[555555];
int a[N], n, lim, d, res;

int BS(int key) {
    int l = 1, r = d, k, m;
    while (l<=r) {
        m = (l+r) >> 1;
        if (f[m].first <= key) {
            k = m; l = m+1;
        }
        else r = m-1;
    }
    return k;
}

void Gen(int i, int x, int y) {
    if (i>lim) {
        d++;
        f[d].first = x-y;
        f[d].second = x;
        return;
    }
    Gen(i+1, x, y);
    Gen(i+1, x+a[i], y);
    Gen(i+1, x, y+a[i]);
}

void Back(int i, int x, int y) {
    if (i>n) {
        int t = BS(y-x);
        if (f[t].first==(y-x))
        res = max(res, f[t].second+x);
        return;
    }
    Back(i+1, x, y);
    Back(i+1, x+a[i], y);
    Back(i+1, x, y+a[i]);
}

int main()
{
    //freopen("DGOLD.in1","r",stdin);
    scanf("%d\n", &n);
    for(int i=1;i<=n;i++) scanf("%d\n", &a[i]);
    lim = n/2;
    Gen(1,0,0);
    sort(f+1, f+d+1);
    Back(lim+1,0,0);
    printf("%d", res);
    return 0;
}

Code mẫu của RR

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>

#define FOR(i,a,b) for(int i=(a),_b=(b); i <= _b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i >= _b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
using namespace std;

int n, a[25], res;

struct HashTable {
    static const int HKEY = 1000003;
    vector< pair<int,int> > h[HKEY];

    inline int key(int s) const {
        int res = s % HKEY;
        if (res < 0) res += HKEY;
        return res;
    }

    void insert(int s, int val) {
        int k = key(s);
        REP(i,h[k].size())
        if (h[k][i].first == s) {
            h[k][i].second = max(h[k][i].second, val);
            return ;
        }
        h[k].push_back(make_pair(s, val));
    }

    int get(int s) {
        int k = key(s);
        REP(i,h[k].size()) {
            if (h[k][i].first == s)
                return h[k][i].second;
        }
        return -1000111000;
    }
} best;

void attempt1(int i, int r, int sum1, int sum2) {
    if (i > r) {
        best.insert(sum1 - sum2, sum1);
        return ;
    }

    attempt1(i+1, r, sum1, sum2);
    attempt1(i+1, r, sum1+a[i], sum2);
    attempt1(i+1, r, sum1, sum2+a[i]);
}

void attempt2(int i, int r, int sum1, int sum2) {
    if (i > r) {
        res = max(res, best.get(sum2-sum1) + sum1);
        return ;
    }

    attempt2(i+1, r, sum1, sum2);
    attempt2(i+1, r, sum1+a[i], sum2);
    attempt2(i+1, r, sum1, sum2+a[i]);
}

int main() {
    scanf("%d", &n); FOR(i,1,n) scanf("%d", &a[i]);
    int l1 = 1, r1 = n/2;
    int l2 = r1+1, r2 = n;

    attempt1(l1, r1, 0, 0);
    attempt2(l2, r2, 0, 0);
    cout << res << endl;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
//#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;

const ld PI = acos(-1.0);
const ld eps = 1e-9;

const int dr[] = {0, +1, 0, -1};
const int dc[] = {+1, 0, -1, 0};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;

int n, a[25], b[25], m, Ma[(1 << 13) + 5], Mb[(1 << 13) + 5];
vector<II> Vt, V;
vector<int> V1, V2;

bool comp(const II &P0,const II &P1){
    if(P0.fi == P1.fi) return P0.se > P1.se;
    return P0.fi < P1.fi;
}

int  main()
{
//  freopen("in.txt", "r", stdin);
    cin >> n;
    Rep(i, n) cin >> a[i];
    m = n - n / 2;
    n = n / 2;
    Rep(i, m) b[i] = a[n + i];
    int res = 0, mask2, T1, T2, T;

    Rep(mask, (1 << n)){
        Ma[mask] = 0;
        Rep(i, n) Ma[mask] += getbit(mask, i) * a[i];
    }

    Rep(mask, (1 << m)){
        Mb[mask] = 0;
        Rep(i, m) Mb[mask] += getbit(mask, i) * b[i];
    }

    Rep(mask, (1 << n)){
        for(int mask1 = mask; mask1 > 0; mask1 = mask & (mask1 - 1)){
            mask2 = mask ^ mask1;
            T1 = Ma[mask1]; T2 = Ma[mask2];
            if(T1 == T2) res = max(res, T1 + T2);
            T = abs(T1 - T2);
            Vt.pb(mp(T, T1 + T2));
        }
    }

    sort(all(Vt), comp);
    V.pb(Vt[0]);
    for(int i = 1; i < sz(Vt); i++){
        if(Vt[i].fi > Vt[i - 1].fi) V.pb(Vt[i]);
    }

    int size = sz(V), t;
    Rep(i, size){
        V1.pb(V[i].fi);
        V2.pb(V[i].se);
    }

    Rep(mask, (1 << m)){
        for(int mask1 = mask; mask1 > 0; mask1 = mask & (mask1 - 1)){
            mask2 = mask ^ mask1;
            T1 = Mb[mask1]; T2 = Mb[mask2];
            if(T1 == T2) res = max(res, T1 + T2);
            T = abs(T1 - T2);
            t = lower_bound(all(V1), T) - V1.begin();
            if(t < size && V1[t] == T){
                res = max(res, V2[t] + T1 + T2);
            }
        }
    }

    cout << res / 2 << endl;
//  cout << clock() << endl;
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.