#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
const int MOD = 1e9+9;
int n,k,c;
int a[505][7];
int p[505];
struct config
{
int sumval,hsh;
int alesi[6];
void schimba(int poz, int newv)
{
hsh = (hsh + MOD - p[alesi[poz]])%MOD;
alesi[poz] = newv;
hsh = (hsh + p[alesi[poz]])%MOD;
sumval=0;
for(int i=1;i<=k;i++)
{
int val=0;
for(int j=0;j<k;j++)
val = max(val, a[alesi[j]][i]);
sumval += val;
}
}
bool is_ales(int x)
{
for(int i=0;i<k;i++)
if(alesi[i]==x)
return 1;
return 0;
}
};
bool operator<(config x, config y)
{
if(x.sumval < y.sumval)
return 1;
if(x.sumval > y.sumval)
return 0;
return x.hsh < y.hsh;
}
bool ales[505];
config init;
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k>>c;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
cin>>a[i][j];
}
}
assert(k<=n);
p[0]=1;
p[1]=997;
for(int i=2;i<=n;i++)
p[i] = 1LL*p[i-1]*p[1]%MOD;
for(int i=1;i<=k;i++)
{
int mxm=-1,unde=-1;
for(int j=1;j<=n;j++)
{
if(!ales[j] && a[j][i]>mxm)
{
mxm = a[j][i];
unde = j;
}
}
assert(unde!=-1);
ales[unde] = 1;
init.schimba(i-1,unde);
}
priority_queue<config> pq;
unordered_map<int,int> inq;
pq.push(init);
inq[init.hsh]=1;
int cnt=0;
while(!pq.empty())
{
config aux = pq.top();
pq.pop();
cnt++;
if(cnt==c)
{
cout<<aux.sumval;
return 0;
}
for(int s=0;s<k;s++)
{
int vechi = aux.alesi[s];
for(int i=1;i<=n;i++)
{
if(!aux.is_ales(i))
{
aux.schimba(s,i);
if(aux.sumval <= aux.sumval && !inq[aux.hsh])
{
pq.push(aux);
inq[aux.hsh]=1;
}
}
}
aux.schimba(s,vechi);
}
}
assert(0==1);
return 0;
}
/*
5 4 4
7 0 4 9
3 0 8 4
1 1 3 7
5 1 3 4
4 2 2 9
*/
Compilation message
olympiads.cpp: In function 'int main()':
olympiads.cpp:98:35: warning: self-comparison always evaluates to true [-Wtautological-compare]
98 | if(aux.sumval <= aux.sumval && !inq[aux.hsh])
| ~~~~~~~~~~ ^~ ~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
9784 KB |
Output is correct |
2 |
Correct |
96 ms |
9652 KB |
Output is correct |
3 |
Correct |
116 ms |
9652 KB |
Output is correct |
4 |
Correct |
92 ms |
9712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
9156 KB |
Output is correct |
2 |
Correct |
45 ms |
9252 KB |
Output is correct |
3 |
Correct |
49 ms |
9152 KB |
Output is correct |
4 |
Correct |
46 ms |
9144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2075 ms |
259076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
9784 KB |
Output is correct |
2 |
Correct |
96 ms |
9652 KB |
Output is correct |
3 |
Correct |
116 ms |
9652 KB |
Output is correct |
4 |
Correct |
92 ms |
9712 KB |
Output is correct |
5 |
Correct |
54 ms |
9156 KB |
Output is correct |
6 |
Correct |
45 ms |
9252 KB |
Output is correct |
7 |
Correct |
49 ms |
9152 KB |
Output is correct |
8 |
Correct |
46 ms |
9144 KB |
Output is correct |
9 |
Execution timed out |
2075 ms |
259076 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |