#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int lim = 1e5; //RYCT
stack<ll> st[lim];
set<ll> visited[lim];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, C; cin >> n >> k >> C;
vector<vector<int>> v(n, vector<int>(k, 0));
for(int i = 0; i < n; i++) for(int j = 0; j < k; j++) cin >> v[i][j];
vector<vector<int>> o(k, vector<int>(n)), pos(k, vector<int>(n));
for(int i = 0; i < k; i++){
for(int j = 0; j < n; j++) o[i][j] = j;
sort(o[i].begin(), o[i].end(), [&](int a, int b){
return v[a][i] > v[b][i];
});
for(int j = 0; j < n; j++) pos[i][o[i][j]] = j;
}
auto vec_to_ll = [&](vector<int> s){
sort(s.begin(), s.end());
ll x = 0, p = 1;
for(int i = 0; i < k; i++) x+=p*s[i], p*=n;
return x;
};
auto ll_to_vec = [&](ll x){
vector<int> s;
for(int i = 0; i < k; i++) s.pb(x%n), x/=n;
return s;
};
auto vec_sum = [&](vector<int> s){
int x = 0;
vector<int> mx(k, 0);
for(int i: s){
assert(i >= 0);
assert(i < n);
for(int j = 0; j < k; j++) mx[j] = max(mx[j], v[i][j]);
}
for(int y: mx) x+=y;
return x;
};
set<ll> values;
{
vector<int> sth;
for(int i = 0; i < k; i++) for(int j = 0; j < n; j++){
if(find(sth.begin(), sth.end(), o[i][j]) != sth.end()) continue;
sth.pb({o[i][j]});
break;
}
int X = vec_sum(sth);
ll x = vec_to_ll(sth);
values.insert(-X);
st[X].push(x);
visited[X].insert(x);
}
int cnt = 0;
while(!values.empty()){
int X = *values.begin();
values.erase(values.begin());
X*=-1;
while(!st[X].empty()){
auto cur = ll_to_vec(st[X].top());
st[X].pop();
cnt++;
if(cnt == C){
cout << X << "\n";
exit(0);
}
vector<int> op;
for(int i = 0; i < k; i++){
vector<int> p;
for(int x: cur) p.pb(pos[i][x]);
sort(p.begin(), p.end());
for(int j = 0; j < k; j++) if((j == k-1 && p[j] != n-1) || p[j+1] > p[j]+1){
op.pb(o[i][p[j]+1]);
}
}
for(int i = 0; i < k; i++) for(int j: op){
auto _cur = cur;
_cur[i] = j;
int Y = vec_sum(_cur);
if(Y > X) continue;
ll x = vec_to_ll(_cur);
if(visited[Y].find(x) == visited[Y].end()){
visited[Y].insert(x);
st[Y].push(x);
}
if(Y < X) values.insert(-Y);
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
94 ms |
146520 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
96 ms |
146424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
89780 KB |
Output is correct |
2 |
Correct |
85 ms |
72852 KB |
Output is correct |
3 |
Correct |
231 ms |
86252 KB |
Output is correct |
4 |
Correct |
219 ms |
86308 KB |
Output is correct |
5 |
Correct |
188 ms |
78008 KB |
Output is correct |
6 |
Runtime error |
108 ms |
147136 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
94 ms |
146520 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |