Submission #1189981

#TimeUsernameProblemLanguageResultExecution timeMemory
1189981crafticatOlympiads (BOI19_olympiads)C++20
44 / 100
1607 ms327680 KiB
#include <bits/stdc++.h>

using namespace std;
#define F0R(i, n) for (int i = 0; i < n;i++)
#define FOR(i, j, n) for(int i = j; i < n;i++)

template<typename T>
using V = vector<T>;
using vi = V<int>;

constexpr int INF = 1e9;

V<vi> v;
int n, k, c;

int eval(vi a) {
    vi opt(k);
    for (auto i : a) {
        F0R(j, k) {
            opt[j] = max(opt[j], v[i][j]);
        }
    }

    return accumulate(opt.begin(), opt.end(), 0, [](int x, int y) {
        return x + y;
    });
}

bool cmp(pair<int, vi> x, pair<int, vi> y) {
    return !(x < y);
}

V<vi> near(vi x) {
    V<vi> ans;
    F0R(i, k) {
        vi copy = x;
        F0R(j, n) {
            copy[i] = j;
            bool valid = 1;
            for (auto v : x) {
                if (v == j) valid = false;
            }

            if (valid) ans.push_back(copy);
        }
    }
    return ans;
}


#ifndef DEBUG
int main() {
    cin >> n >> k >> c;

    v.resize(n, vi(k));
    F0R(i, n) {
        F0R(j, k)
            cin >> v[i][j];
    }

    vi opt(k);
    F0R(i, k) {
        F0R(j, n) {
            if (v[opt[i]][i] < v[j][i]) opt[i] = j;
        }
    }

    sort(opt.begin(), opt.end());
    opt.erase( unique( opt.begin(), opt.end() ), opt.end() );

    while (opt.size() < k) {
        F0R(i, n) {
            bool valid = true;
            for (auto x : opt)
                if (x == i) valid = false;

            if (valid) {
                opt.push_back(i);
                break;
            }
        }
    }

    map<vi, bool> vis;
    priority_queue<pair<int, vi>, V<pair<int, vi>>> pq;

    pq.push({eval(opt), opt});
    int last = 0;

    while (!pq.empty() and c > 0) {
        auto [val, vec] = pq.top(); pq.pop();
        sort(vec.begin(), vec.end());

        if (vis[vec]) continue;
        last = val;
        vis[vec] = 1;
        c--;

        for (auto y : near(vec)) {
            sort(y.begin(), y.end());

            if (vis[y]) continue;
            pq.push({eval(y),y});
        }
    }

    cout << last << endl;
}
#endif

#if DEBUG
int rand(int a, int b) {
    return a + rand() % (b -a  +1);
}

long long MOD = 1e9 + 7;

long long hashMy(vi x) {
    long long ans = 0, mul = 1;
    for (auto v : x) {
        ans += v * mul;
        mul *= 1e6;
        ans %= MOD;
        mul %= MOD;
    }
    return ans;
}

int main() {
    n = 500, k = 6, c = 2000;

    v.resize(n, vi(k));
    int lastT = clock();
    F0R(i, n) {
        F0R(j, k)
            v[i][j] = rand(1, 1e6);
    }

    vi opt(k);
    F0R(i, k) {
        F0R(j, n) {
            if (v[opt[i]][i] < v[j][i]) opt[i] = j;
        }
    }

    sort(opt.begin(), opt.end());
    opt.erase( unique( opt.begin(), opt.end() ), opt.end() );

    while (opt.size() < k) {
        F0R(i, n) {
            bool valid = true;
            for (auto x : opt)
                if (x == i) valid = false;

            if (valid) {
                opt.push_back(i);
                break;
            }
        }
    }

    unordered_map<long long, bool> vis;
    priority_queue<pair<int, vi>, V<pair<int, vi>>> pq;

    pq.push({eval(opt), opt});
    int last = 0;

    while (!pq.empty() and c > 0) {
        auto [val, vec] = pq.top(); pq.pop();
        sort(vec.begin(), vec.end());
        long long h = hashMy(vec);

        if (vis[h]) continue;
        last = val;
        vis[h] = 1;
        c--;

        for (auto y : near(vec)) {
            sort(y.begin(), y.end());
            long long h2 = hashMy(y);
            if (vis[h2]) continue;
            pq.push({eval(y),y});
        }
    }

    cout << last << endl;
    cout << clock() - lastT << endl;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...