QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#687675 | #8005. Crossing the Border | atgc | RE | 0ms | 0kb | C++23 | 2.1kb | 2024-10-29 20:26:55 | 2024-10-29 20:26:56 |
answer
#include<bits/stdc++.h>
#define hbit(x) (31^__builtin_clz(x))
#define csub(x) (1<<__builtin_popcount(x))
using namespace std;
const int mod = 998244353,N=22,p3H=pow(3,N/2);
int n,W;
struct {
int w,c;
}t[N];
struct {
int msk,sumw;
}L[1<<N/2],R[1<<N/2],subR[1<<N/2],Lt[p3H],Rt[p3H];
int Ltbuf[1<<N/2],Rtbuf[1<<N/2];
struct node{int v,c;node adv(int nv){return{v+nv,c};}}f[1<<N/2][1<<N/2],trs[1<<N/2];//R; L
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline node add(node a,node b){return a.v==b.v?node{a.v,add(a.c,b.c)}:a.v<b.v?a:b;}
#define addto(a,b) a=add(a,b)
signed main() {
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>W;for(int i=0;i<n;++i)cin>>t[i].w>>t[i].c;
sort(t,t+n,[](auto a,auto b){return a.c<b.c;});
const int LH=(n+1)>>1,RH=n-LH,LU=1<<LH,RU=1<<RH;
for(int i=0;i<LH;++i)L[1<<i]={1<<i,t[i].w};
for(int i=0;i<RH;++i)R[1<<i]={1<<i,t[i+LH].w};
#define init(L,Lt,Ltbuf,LU)\
for(int S=0;S<LU;++S)\
L[S]={S,min(W+1,L[S&(S-1)].sumw+L[S&-S].sumw)};\
sort(L,L+LU,[](auto a,auto b){return a.sumw<b.sumw;});\
for(int _b=0,S=0;S<LU;++S)\
Ltbuf[S]=_b,_b+=csub(S);\
for(int i=0;i<LU;++i)\
for(int M=L[i].msk,U=(LU-1)^M,S=U,_=-1;_&S?:_++;--S&=U)Lt[Ltbuf[M|S]++]=L[i];
init(L,Lt,Ltbuf,LU)
init(R,Rt,Rtbuf,RU)
memset(f,0x3f,sizeof f);
f[0][0]={0,1};
for(int S=0;S<LU;++S)
for(int i=0;i<LU&&L[i].sumw<=W;++i)
if(int T=L[i].msk;T&&!(T&S)&&hbit(T)==hbit(T|S))
addto(f[0][S|T],f[0][S].adv(t[hbit(T)].c));
for(int RS=1;RS<RU;++RS){
int Utr=RS^1<<hbit(RS);
auto[exw,exc]=t[LH+hbit(RS)];
int sc=csub(Utr);
for(int i=0;i<sc;++i)subR[i]=Rt[(Utr?Rtbuf[Utr-1]:0)+i];
for(int basL=0;basL<LU;++basL){
for(int i=0;i<sc;++i){
trs[i]=f[Utr^subR[i].msk][basL];
if(i)trs[i]=add(trs[i],trs[i-1]);
}
int tp=sc-1;
int Utl=(LU-1)^basL,lc=csub(Utl),buf=(Utl?Ltbuf[Utl-1]:0);
for(int i=0;i<lc;++i){
auto[mskl,sumwl]=Lt[buf++];
while(~tp&&subR[tp].sumw+sumwl+exw>W)--tp;
if(!~tp)break;
addto(f[RS][basL|mskl],trs[tp].adv(exc));
}
}
}
auto[w,c]=f[RU-1][LU-1];
cout<<w<<' '<<c;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 5 3 5 1 4 2 3 2 2 2 1