#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
const int N=505;
const int K=6;
int a[K][N],id[K][N],pos[K][N],n,k,c;
int Val(vector<int> state)
{
int ans=0;
for(int i=0;i<k;i++) ans+=a[i][id[i][state[i]]];
return ans;
}
void Answer(vector<int> state)
{
printf("%i\n",Val(state));
exit(0);
}
int Get(vector<int> state)
{
int ans=0;
for(int i=1;i<=n;i++)
{
bool ok=1;
for(int j=0;j<k;j++) if(pos[j][i]<=state[j]) ok=0;
if(ok) ans++;
}
return ans;
}
void Check(vector<int> state)
{
vector<int> all;
for(int i=0;i<k;i++) all.push_back(id[i][state[i]]);
sort(all.begin(),all.end());
all.resize(unique(all.begin(),all.end())-all.begin());
int dif=k-all.size();
ll del;
if(dif==0) del=1;
else
{
ll tmp=Get(state);
del=1;
for(int i=0;i<dif;i++)
{
del*=tmp-i;
if(del>1000000000) Answer(state);
}
for(int i=1;i<=dif;i++) del/=i;
}
//printf("->");for(int i:all) printf("%i ",i);printf("\n");
//for(int i=0;i<k;i++) printf("%i ",id[i][state[i]]);printf("\n");
//printf("%i %lld\n",Val(state),del);
if(del>=c) Answer(state);
c-=del;
}
bool Ok(vector<int> state)
{
vector<int> all;
for(int i=0;i<k;i++) all.push_back(id[i][state[i]]);
sort(all.begin(),all.end());
all.resize(unique(all.begin(),all.end())-all.begin());
int dif=k-all.size();
if(dif==0) return 1;
return dif<=Get(state);
}
bool Bad(vector<int> state, int x)
{
for(int i=0;i<k;i++) if(pos[i][x]<state[i]) return 1;
//for(int i=0;i<k;i++) if(a[i][x]>a[i][id[i][state[i]]]) return 1;
return 0;
}
vector<int> Next(vector<int> state, int t)
{
for(int i=0;i<k;i++) if(i!=t && id[i][state[i]]==id[t][state[t]]) state[i]++;
state[t]++;
for(int i=0;i<k;i++)
{
while(state[i]<=n && Bad(state,id[i][state[i]])) state[i]++;
if(state[i]>n) return vector<int>(0);
}
return state;
}
set<string> was;
string ToString(vector<int> state)
{
string ans;
for(int i=0;i<k;i++) ans+=(char)state[i];
return ans;
}
int main()
{
scanf("%i %i %i",&n,&k,&c);
for(int i=1;i<=n;i++) for(int j=0;j<k;j++) scanf("%i",&a[j][i]),id[j][i]=i;
for(int j=0;j<k;j++) sort(id[j]+1,id[j]+1+n,[&](int x, int y){ return mp(a[j][x],x)>mp(a[j][y],y);});
for(int j=0;j<k;j++) for(int i=1;i<=n;i++) pos[j][id[j][i]]=i;
priority_queue<pair<int,vector<int>>> pq;
vector<int> st;
for(int j=0;j<k;j++) st.push_back(1);
auto Push=[&](vector<int> state)
{
if(Ok(state))
{
string s=ToString(state);
if(!was.count(s))
{
pq.push({Val(state),state});
was.insert(s);
}
}
};
Push(st);
while(pq.size())
{
vector<int> state=pq.top().second;
pq.pop();
Check(state);
for(int i=0;i<k;i++)
{
vector<int> nxt=Next(state,i);
if(nxt.size()) Push(nxt);
}
}
printf(":D\n");
return 0;
}
Compilation message
olympiads.cpp: In function 'int main()':
olympiads.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&n,&k,&c);
~~~~~^~~~~~~~~~~~~~~~~~~~~
olympiads.cpp:93:65: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) for(int j=0;j<k;j++) scanf("%i",&a[j][i]),id[j][i]=i;
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
508 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
14 ms |
988 KB |
Output is correct |
4 |
Correct |
15 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
424 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
20 ms |
1524 KB |
Output is correct |
4 |
Correct |
21 ms |
1912 KB |
Output is correct |
5 |
Correct |
6 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
508 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
404 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
14 ms |
988 KB |
Output is correct |
8 |
Correct |
15 ms |
1016 KB |
Output is correct |
9 |
Correct |
5 ms |
424 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
20 ms |
1524 KB |
Output is correct |
12 |
Correct |
21 ms |
1912 KB |
Output is correct |
13 |
Correct |
6 ms |
632 KB |
Output is correct |
14 |
Correct |
7 ms |
632 KB |
Output is correct |
15 |
Correct |
20 ms |
1144 KB |
Output is correct |
16 |
Correct |
8 ms |
760 KB |
Output is correct |
17 |
Incorrect |
22 ms |
1908 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |