QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115150 | #5810. Doubly-sorted Grid | DerekFeng | 10 | 11ms | 10036kb | C++23 | 1.4kb | 2023-06-24 18:05:05 | 2023-06-24 18:05:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=10007;
int n,m,N;char s[12][12];
int mp[1<<20],msk[20000];
int nx[20000][22];bool ok[20000][26];
int dp[20000],ld[26][12],rd[26][12],mt[12];
void add(int &x,int y){if((x+=y)>=M)x-=M;}
void sol(){
scanf("%d%d",&n,&m),N=0;
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
for(int c=0;c<26;c++)for(int i=1;i<=n;i++){
ld[c][i]=0;for(int j=1;j<=m;j++)if(s[i][j]!='.'&&s[i][j]<=c+'a')ld[c][i]=j;
rd[c][i]=m;for(int j=m;j;j--)if(s[i][j]!='.'&&s[i][j]>c+'a')rd[c][i]=j-1;
}
memset(mp,0,sizeof(mp));
for(int i=0;i<(1<<n+m);i++)if(__builtin_popcount(i)==n)mp[i]=++N,msk[N]=i;
memset(nx,0,sizeof(nx));
for(int i=1;i<=N;i++){
for(int j=0;j<n+m;j++)if(msk[i]>>j&1){
int k;for(k=j+1;msk[i]>>k&1;k++);
if(k==n+m)nx[i][j]=0;else nx[i][j]=mp[msk[i]^(1<<j)^(1<<k)];
}
for(int j=0,k=0;j<n+m;j++){if(msk[i]>>j&1)mt[n-j+k]=k;else k++;}
for(int c=0;c<26;c++){
ok[i][c]=1;for(int j=1;j<=n;j++)
ok[i][c]&=ld[c][j]<=mt[j],ok[i][c]&=mt[j]<=rd[c][j];
}
}
memset(dp,0,sizeof(dp)),dp[1]=1;
for(int c=0;c<26;c++){
for(int i=0;i<n+m;i++)for(int j=1;j<=N;j++)
if(nx[j][i])add(dp[nx[j][i]],dp[j]);
for(int j=1;j<=N;j++)if(!ok[j][c])dp[j]=0;
}
printf("%d\n",dp[N]);
}
int main(){
int tc;scanf("%d",&tc);
for(int tt=1;tt<=tc;tt++)
printf("Case #%d: ",tt),sol();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 11ms
memory: 10036kb
input:
40 4 4 .... .... .... .... 4 4 abcd bcde cdef defg 4 4 bbcc ccde cdef deef 4 4 .... .... .r.. ..e. 3 4 .... .... ...g 1 3 f.. 2 2 .b bb 2 3 ... ..f 4 4 .... .... ..t. ..d. 3 3 .gn ... ... 4 4 ...a ...b ...c ...d 4 4 ...b ..bb .bbb bbbb 4 4 a... .b.. ..c. ...d 3 3 c.. glw ..w 1 4 .... 1 3 d.v 4 2 .. ...
output:
Case #1: 7285 Case #2: 1 Case #3: 1 Case #4: 0 Case #5: 718 Case #6: 231 Case #7: 2 Case #8: 686 Case #9: 0 Case #10: 3500 Case #11: 330 Case #12: 14 Case #13: 4096 Case #14: 2756 Case #15: 3737 Case #16: 19 Case #17: 8175 Case #18: 3737 Case #19: 7279 Case #20: 256 Case #21: 23 Case #22: 7569 Case ...
result:
ok 40 lines
Subtask #2:
score: 0
Runtime Error
Test #2:
score: 0
Runtime Error
input:
40 5 10 .......... .......... .......... .......... .......... 10 10 ....a..... ....c..... ....g..... ....j..... ....l..... ....m..... ....n..... ....q..... ....t..... ....v..... 10 10 .......... .......... .......... .......... .......... .......... .......... .......... ...ok..... .......... 9 10 ...