QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#358533 | #6194. Knapsack Problem | schrodingerstom | TL | 84ms | 4800kb | C++14 | 3.4kb | 2024-03-19 20:40:27 | 2024-03-19 20:40:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
bool memBeg;
template<typename T> void chkmin(T &x,T y) {if(x>y) x=y;}
template<typename T> void chkmax(T &x,T y) {if(x<y) x=y;}
constexpr int mod=1e9+7;
void inc(int &x,int y) {x+=y; x-=(x>=mod)*mod;}
void dec(int &x,int y) {x-=y; x+=(x<0)*mod;}
constexpr int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
constexpr int sub(int x,int y) {return (x<y)?x-y+mod:x-y;}
constexpr int quick_pow(int x,int times) {
int ret=1;
for(;times;times>>=1,x=1ll*x*x%mod) {
if(times&1) ret=1ll*ret*x%mod;
}
return ret;
}
constexpr int D=30;
// constexpr int D=4;
constexpr int maxn=25;
constexpr int maxK=10;
constexpr int maxmask1=5005;
constexpr int maxmask2=6.6e4+5;
constexpr ll INF=0x3f3f3f3f3f3f3f3f;
int K,c[maxn],a[maxK];
ll maxx[maxmask2],nmaxx[maxmask2];
void mian() {
scanf("%d",&K);
for(int i=1;i<(1<<K);i++) scanf("%d",&c[i]);
for(int i=0;i<K;i++) scanf("%d",&a[i]);
int full1=1,full2=1;
for(int i=0;i<K;i++) {
full1<<=(K-1); full2<<=K;
}
fill(maxx,maxx+full1,-INF);
maxx[0]=0ll;
for(int i=D-1;i>=0;i--) {
fill(maxx+full1,maxx+full2,-INF);
for(int j=full1-1;j>=0;j--) {
ll now=maxx[j]; maxx[j]=-INF;
if(now<0) continue;
int nj=0,pj=j;
for(int k=0;k<K;k++) {
nj=((pj&((1<<(K-1))-1))<<1|((a[k]>>i)&1))<<(k*K)|nj;
pj>>=(K-1);
}
maxx[nj]=now;
}
for(int j=1;j<(1<<K);j++) {
fill(nmaxx,nmaxx+full2,-INF);
for(int k=0;k<full2;k++) {
if(maxx[k]<0) continue;
chkmax(nmaxx[k],maxx[k]);
int nk=k;
for(int t=0;t<K;t++) {
if(j&(1<<t)) {
if((nk>>(t*K))&((1<<K)-1)) nk-=1<<(t*K);
else {nk=-1; break;}
}
}
if(nk!=-1) chkmax(nmaxx[nk],maxx[k]+(((ll)c[j])<<i));
// if(nk!=-1&&(maxx[k]+(((ll)c[j])<<i)==81||maxx[k]+(((ll)c[j])<<i)==77)) {
// printf("i = %d, j = %d, k = %d, nk = %d, maxx[k] = %lld\n",i,j,k,nk,maxx[k]);
// }
}
memcpy(maxx,nmaxx,sizeof(ll)*full2);
}
fill(nmaxx,nmaxx+full1,-INF);
// printf("i = %d: \n",i);
for(int j=0;j<full2;j++) {
int nj=0;
for(int t=K-1;t>=0;t--) {
int cur=(j>>(t*K))&((1<<K)-1);
if(cur>=(1<<(K-1))) {nj=-1; break;}
nj=nj<<(K-1)|cur;
}
if(nj!=-1) chkmax(nmaxx[nj],maxx[j]);
// if(nj!=-1) {
// for(int t=0;t<K;t++) {
// int cur=(j>>(t*K))&((1<<K)-1);
// printf("%d ",cur);
// }
// printf(": upd = %lld\n",maxx[j]);
// }
}
memcpy(maxx,nmaxx,sizeof(ll)*full1);
}
printf("%lld\n",max(-1ll,maxx[0]));
}
bool memEn;
void fl() {
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int main() {
fprintf(stderr,"%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
int _; scanf("%d",&_);
while(_--) mian();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 84ms
memory: 4800kb
input:
3 2 1 2 4 4 5 3 3226252 19270256 2430652 1187613 12496062 10286165 17494834 24 85 34 4 901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555 529321239 218214127 92942310 207467810
output:
18 1949753378 7832404777567179
result:
ok 3 number(s): "18 1949753378 7832404777567179"
Test #2:
score: -100
Time Limit Exceeded
input:
100 4 4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493 200477197 258791439 590664017 982949628 4 5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...