#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 505, Kmax = 8;
map<ll, int> val;
int a[Kmax][Nmax];
vector<int> ord[Kmax];
int n, k, C;
bool used[Nmax];
struct myCmp
{
bool operator () (ll x, ll y)
{
return (x == y ? x > y : val[x] < val[y]);
}
};
priority_queue<ll, vector<ll>, myCmp> heap;
ll pwN[Kmax];
void next_state(vector<int> aux, int pos)
{
int i, j;
// cerr << pos << "#";
// for(auto it : aux) cerr << it << ' '; cerr << '\n';
for(i=0; i<pos; ++i)
{
assert(!used[ord[i][aux[i]]]);
used[ord[i][aux[i]]] = 1;
}
++aux[pos];
while(aux[pos] < n && used[ord[pos][aux[pos]]]) ++aux[pos];
if(aux[pos] == n)
{
for(i=0; i<pos; ++i) used[ord[i][aux[i]]] = 0;
return;
}
used[ord[pos][aux[pos]]] = 1;
for(i=pos+1; i<k; ++i)
{
while(aux[i] < n && used[ord[i][aux[i]]]) ++aux[i];
if(aux[i] == n)
{
for(--i; i>=0; --i) used[ord[i][aux[i]]] = 0;
return;
}
used[ord[i][aux[i]]] = 1;
}
ll state = 0;
for(i=0; i<k; ++i)
state += pwN[i] * aux[i];
for(i=0; i<k; ++i) used[ord[i][aux[i]]] = 0;
if(val[state]) return;
for(i=0; i<k; ++i)
{
int best = 0;
for(j=0; j<k; ++j)
best = max(best, a[i][ord[j][aux[j]]]);
val[state] += best;
}
heap.push(state);
// cerr << pos << '\n';
// for(i=0; i<k; ++i) cerr << ord[i][aux[i]] << ' '; cerr << '\n';
}
int main()
{
// freopen("olympiads.in", "r", stdin);
cin.tie(0); cin.sync_with_stdio(false);
cin >> n >> k >> C;
int i, j;
for(i=1; i<=n; ++i)
for(j=0; j<k; ++j)
cin >> a[j][i];
for(i=0; i<k; ++i)
{
for(j=1; j<=n; ++j) ord[i].push_back(j);
auto cmp = [i] (int x, int y)
{
return (a[i][x] > a[i][y]);
};
sort(ord[i].begin(), ord[i].end(), cmp);
}
ll stt = 0;
pwN[0] = 1;
for(i=1; i<k; ++i) pwN[i] = pwN[i-1] * n;
for(i=0; i<k; ++i)
{
for(j=0; j<n; ++j)
if(!used[ord[i][j]]) break;
stt += pwN[i] * j;
used[ord[i][j]] = 1;
}
for(i=1; i<=n; ++i) used[i] = 0;
heap.push(stt);
for(i=0; i<k; ++i) val[stt] += a[i][ord[i][0]];
map< vector<int>, int > mp;
while(C)
{
assert(heap.size());
ll now = heap.top();
heap.pop();
ll cnow = now;
vector<int> aux;
for(i=0; i<k; ++i)
{
aux.push_back(now % n);
now /= n;
}
vector<int> v;
for(i=0; i<k; ++i) v.push_back(ord[i][aux[i]]);
sort(v.begin(), v.end());
if(!mp[v]) mp[v] = 1, --C;
// for(auto it : v) cerr << it << ' '; cerr << '\n';
if(C == 0)
{
cout << val[cnow] << '\n';
return 0;
}
// for(i=0; i<k; ++i)
// cerr << ord[i][aux[i]] << ' '; cerr << '\n';
for(i=0; i<k; ++i)
{
// for(j=1; j<=n; ++j) assert(used[j] == 0);
next_state(aux, i);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
888 KB |
Output is correct |
2 |
Correct |
9 ms |
760 KB |
Output is correct |
3 |
Correct |
13 ms |
888 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2059 ms |
38636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1272 KB |
Output is correct |
2 |
Correct |
15 ms |
1240 KB |
Output is correct |
3 |
Correct |
20 ms |
1272 KB |
Output is correct |
4 |
Correct |
22 ms |
1400 KB |
Output is correct |
5 |
Correct |
60 ms |
2676 KB |
Output is correct |
6 |
Execution timed out |
2052 ms |
34412 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
888 KB |
Output is correct |
2 |
Correct |
9 ms |
760 KB |
Output is correct |
3 |
Correct |
13 ms |
888 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
5 |
Execution timed out |
2059 ms |
38636 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |