#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);
}
vi imp;
V<vi> near(vi x) {
V<vi> ans;
F0R(i, k) {
vi copy = x;
for (auto j : imp) {
copy[i] = j;
bool valid = 1;
for (auto v : x) {
if (v == j) valid = false;
}
if (valid) ans.push_back(copy);
}
}
return ans;
}
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;
}
#ifndef DEBUG
int main() {
cin >> n >> k >> c;
v.resize(n, vi(k));
F0R(i, n) {
F0R(j, k)
cin >> v[i][j];
}
V<set<pair<int,int>>> opts(k);
F0R(i, k) {
F0R(j, n) {
opts[j].insert({v[j][k], k});
}
}
vi opt(k);
F0R(i, k) {
opt[i] = opts[i].rbegin()->second;
int iter = 0;
for (auto rit = opts[i].rbegin(); rit != opts[i].rend(); rit++)
if (iter++ < 15)
imp.push_back(rit->second);
}
sort(imp.begin(), imp.end());
opt.erase( unique( imp.begin(), imp.end() ), imp.end() );
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;
}
#endif
#if DEBUG
int rand(int a, int b) {
return a + rand() % (b -a +1);
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |