This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int n, k;
int mat[505][10];
vector <pair <int, int>> best[10];
struct SearchSpace{
int score = 0;
vector <int> tak;
bitset <505> zab;
bitset <505> used;
int fix = 0;
int sum[7];
void eval(const int _fix, const vector <int> &_tak, const bitset <505> &_zab){
tak = _tak;
zab = _zab;
used.reset();
for(auto c : tak) used[c] = 1;
fix = _fix;
for(int i=1; i<=k; i++) sum[i] = 0;
for(int i=fix+1; i<=k; i++){
bool found = 0;
for(auto c : best[i]){
int tk = c.second;
if(used[tk] || zab[tk]) continue;
found = 1;
used[tk] = 1;
tak.push_back(tk);
break;
}
if(!found){
score = -1;
return;
}
}
for(auto c : tak){
for(int j=1; j<=k; j++){
sum[j] = max(sum[j], mat[c][j]);
}
}
score = 0;
for(int j=1; j<=k; j++) score += sum[j];
}
bool operator <(const SearchSpace &s) const { return score < s.score; }
};
bitset <505> def;
vector <int> empt;
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int m;
cin >> n >> k >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=k; j++){
cin >> mat[i][j];
best[j].push_back({mat[i][j], i});
}
}
for(int i=1; i<=k; i++){
sort(best[i].begin(), best[i].end());
reverse(best[i].begin(), best[i].end());
}
priority_queue <SearchSpace> pq;
SearchSpace prvi;
prvi.eval(0, empt, def);
pq.push(prvi);
while(m){
SearchSpace sp = pq.top();
pq.pop();
m--;
if(m == 0){
cout << sp.score << "\n";
return 0;
}
vector <int> tkk;
bitset <505> zab = sp.zab;
for(int i=0; i<sp.fix; i++) tkk.push_back(sp.tak[i]);
for(int fix=sp.fix; fix<k; fix++){
SearchSpace novi;
zab[sp.tak[fix]] = 1;
novi.eval(fix, tkk, zab);
zab[sp.tak[fix]] = 0;
pq.push(novi);
tkk.push_back(sp.tak[fix]);
}
}
return 0;
}
# | 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... |