This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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]/250);
ans+=(char)(state[i]%250);
}
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 (stderr)
olympiads.cpp: In function 'int main()':
olympiads.cpp:96: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:97: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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |