#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef pair<int,ll> il;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 505
int n,k,q,a[maxn][6],b[maxn][6],c[6],memo[maxn][64][6];
vector<int> d[6];
set<il> s;
int dp(int i,int msk,int rem){
if(i==n)return rem==0&&msk==(1<<k)-1;
if(memo[i][msk][rem]!=-1)return memo[i][msk][rem];
int ans=dp(i+1,msk,rem);
if(rem){
int nmsk=msk;
bool pos=true;
for(int j=0;j<k;++j){
if(b[i][j]==c[j])nmsk^=(1<<j);
else if(b[i][j]>c[j])pos=false;
}
if(pos)ans+=dp(i+1,nmsk,rem-1);
}
return memo[i][msk][rem]=ans;
}
int main(){
sf("%d%d%d",&n,&k,&q);
for(int i=0;i<n;++i){
for(int j=0;j<k;++j){
sf("%d",&a[i][j]);
d[j].pb(a[i][j]);
}
}
int tot=0;ll cur=0;
for(int j=0;j<k;++j){
disc(d[j]);
cur*=500;
cur+=sz(d[j])-1;
tot+=d[j][sz(d[j])-1];
}
s.insert({tot,cur});
for(int i=0;i<n;++i){
for(int j=0;j<k;++j){
b[i][j]=lb(d[j],a[i][j]);
}
}
while(!s.empty()){
auto[sm,cur]=*--s.end();
s.erase(--s.end());
ll tcur=cur;
for(int j=k-1;j>=0;--j){
c[j]=tcur%500;
tcur/=500;
}
//pf("cur %d: ",sm);
//for(int j=0;j<k;++j)pf("%d ",c[j]);
//pf("\n");
memset(memo,-1,sizeof memo);
q-=dp(0,0,k);
//pf("q: %d\n",q);
if(q<=0){
pf("%d\n",sm);
return 0;
}
ll mult=1;
for(int j=k-1;j>=0;--j){
if(c[j]!=0)s.insert({sm-(d[j][c[j]]-d[j][c[j]-1]),cur-mult});
mult*=500;
}
}
pf("no answer :(\n");
}
/*
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:86:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | sf("%d%d%d",&n,&k,&q);
| ^
olympiads.cpp:89:6: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | sf("%d",&a[i][j]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
1128 KB |
Output is correct |
2 |
Correct |
25 ms |
1112 KB |
Output is correct |
3 |
Correct |
2 ms |
1108 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
1108 KB |
Output is correct |
2 |
Correct |
37 ms |
1292 KB |
Output is correct |
3 |
Correct |
74 ms |
1356 KB |
Output is correct |
4 |
Incorrect |
119 ms |
1424 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1108 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
1128 KB |
Output is correct |
2 |
Correct |
25 ms |
1112 KB |
Output is correct |
3 |
Correct |
2 ms |
1108 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |