#include <bits/stdc++.h>
using namespace std;
#define ll long long
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;
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 a[j][x]>a[j][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:87: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:88:68: 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;
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
436 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
15 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
21 ms |
1604 KB |
Output is correct |
4 |
Correct |
24 ms |
1984 KB |
Output is correct |
5 |
Correct |
4 ms |
380 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
436 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
14 ms |
1016 KB |
Output is correct |
8 |
Correct |
15 ms |
1016 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
21 ms |
1604 KB |
Output is correct |
12 |
Correct |
24 ms |
1984 KB |
Output is correct |
13 |
Correct |
4 ms |
380 KB |
Output is correct |
14 |
Correct |
6 ms |
504 KB |
Output is correct |
15 |
Correct |
20 ms |
1144 KB |
Output is correct |
16 |
Correct |
9 ms |
760 KB |
Output is correct |
17 |
Incorrect |
24 ms |
1908 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |