83632024-01-15 00:23:12TuruTamasFestés (50 pont)cpp17Time limit exceeded 10/50705ms132544 KiB
// a kurva anyját ennek a feladatnak, baszódjon meg!

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

#ifdef DEBUG
ifstream in_file("minta/be2.txt");
#define input in_file
#define INTHENAMEOFGOD
#else
#define input cin
#define INTHENAMEOFGOD \
    ios::sync_with_stdio(0); \
    cin.tie(0); \
    cout.tie(0);
#endif
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef array<ll, 2> pii;

ll N, M, a, ans = LLONG_MAX;
vector<vector<vector<ll>>> vvs;
vector<vvi> C;
vi R;
vector<ll> s;
vector<ll> festett;

void f(ll i, vector<vector<ll>> &v) {
    vector<vector<ll>> ret;
    for (ll l = 0; l <= i; l++) {
        for (ll r = i; r < N; r++) {
            vector<ll> s;
            for (ll i = l; i <= r; i++)
                s.push_back(i);
            v.push_back(s);
        }
    }
}

map<tuple<vector<ll>, vector<ll>::iterator, ll>, ll> mvvillll;

ll g(vector<ll>::iterator it, ll m) {
    if (it == s.end())
        return 0;
    if (find(festett.begin(), festett.end(), *it) != festett.end())
        return g(++it, m);

    vector<ll> lefestetlen;
    for (ll val : s) {
        if(find(festett.begin(), festett.end(), val) == festett.end()) {
            lefestetlen.push_back(val);
        }
    }
    sort(lefestetlen.begin(), lefestetlen.end());
    if (mvvillll.count({lefestetlen, it, m})) {
        return mvvillll[{lefestetlen, it, m}];
    }

    ll r = LLONG_MAX;
    for (auto range : vvs[*it]) {
        ll first = *range.begin();
        ll last = *range.rbegin();
        ll to_remove = festett.size();
        for (ll val : range) {
            festett.push_back(val);
        }
        to_remove = festett.size() - to_remove;
        auto nit = it;
        nit++;
        r = min(r, C[m][first][last] + g(nit, m));
        for (ll i = 0; i < to_remove; i++)
            festett.pop_back();
    }

    mvvillll[{lefestetlen, it, m}] = r;
    return r;
}

void call_g(ll i) {
    if (i == N) {
        ll ans_tmp = 0;
        for (ll m = 0; m < M; m++) {
            ans_tmp += g(s.begin(), m);
        }
        for (ll sor = 0; sor < N; sor++) {
            if (find(s.begin(), s.end(), sor) == s.end())
                ans_tmp += R[sor];
        }
        ans = min(ans, ans_tmp);
    } else {
        call_g(i+1);
        s.push_back(i);
        call_g(i+1);
        s.pop_back();
    }
}

int main() {
    INTHENAMEOFGOD
    input >> N >> M;

    R.resize(N);
    for (ll &val : R)
        input >> val;

    C.resize(M);
    for (ll m = 0; m < M; m++) {
        C[m].resize(N);
        for (ll l = 0; l < N; l++) {
            C[m][l].resize(N);
            for (ll r = l; r < N; r++) {
                input >> C[m][l][r];
            }
        }
    }

    vvs.resize(N);
    for (ll n = 0; n < N; n++) {
        f(n, vvs[n]);
    }

    call_g(0);
    cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
base10/50
1Accepted0/03ms1892 KiB
2Accepted0/03ms2252 KiB
3Time limit exceeded0/2705ms73948 KiB
4Accepted2/27ms3208 KiB
5Accepted3/354ms10184 KiB
6Time limit exceeded0/2643ms79188 KiB
7Time limit exceeded0/2671ms74484 KiB
8Time limit exceeded0/2632ms72204 KiB
9Time limit exceeded0/2675ms74612 KiB
10Time limit exceeded0/2663ms74220 KiB
11Time limit exceeded0/2663ms74176 KiB
12Time limit exceeded0/2684ms72756 KiB
13Time limit exceeded0/2656ms70712 KiB
14Accepted2/2572ms132516 KiB
15Accepted3/3573ms132544 KiB
16Time limit exceeded0/3671ms71704 KiB
17Time limit exceeded0/2663ms71272 KiB
18Time limit exceeded0/3675ms73360 KiB
19Time limit exceeded0/2684ms72268 KiB
20Time limit exceeded0/2663ms72880 KiB
21Time limit exceeded0/2656ms73668 KiB
22Time limit exceeded0/2660ms74420 KiB
23Time limit exceeded0/2675ms75184 KiB
24Time limit exceeded0/2667ms74620 KiB
25Time limit exceeded0/2704ms77000 KiB