#include <bits/stdc++.h> using namespace std; const int MX=400,md=1000000007; short cur,f[MX][MX][MX+MX][2][2],mx[MX][MX]; int n,m,a[MX],b[MX],res[MX+MX]; void upd(short& x, short v) { if (v<x) x=v; } int main() { scanf("%d%d",&n,&m); for (int i=0; i<n; i++) scanf("%d",&a[i]); for (int i=0; i<m; i++) scanf("%d",&b[i]); for (int sti=0; sti<n; sti++) for (int stj=0; stj<m; stj++) { for (int i=sti; i<=n; i++) for (int j=stj; j<=m; j++) { memset(f[i][j],126,sizeof(f[i][j])); mx[i][j]=0; } int i=sti,j=stj; short inf=f[i][j][0][0][0]; f[i+1][j+1][1][int(a[i]>b[j])][0]=2; f[i+1][j+1][1][int(a[i]<b[j])][1]=2; mx[i+1][j+1]=1; if (i+1<n) { f[i+2][j][1][int(a[i+1]>a[i])][0]=2; mx[i+2][j]=1; } if (j+1<m) { f[i][j+2][1][int(b[j+1]>b[j])][1]=2; mx[i][j+2]=1; } for (int i=sti; i<=n; i++) for (int j=stj; j<=m; j++) { short best=n+m; for (int k=0; k<=mx[i][j]; k++) for (int up=0; up<2; up++) { short cur=f[i][j][k][up][0]; if (cur<inf) { best=min(best,cur); if (i<n) { short nup=(a[i]>a[i-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i+1][j][nk][nup][0],len); mx[i+1][j]=max(mx[i+1][j],nk); } if (j<m) { short nup=(b[j]>a[i-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i][j+1][nk][nup][1],len); mx[i][j+1]=max(mx[i][j+1],nk); } } cur=f[i][j][k][up][1]; if (cur<inf) { best=min(best,cur); if (i<n) { short nup=(a[i]>b[j-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i+1][j][nk][nup][0],len); mx[i+1][j]=max(mx[i+1][j],nk); } if (j<m) { short nup=(b[j]>b[j-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i][j+1][nk][nup][1],len); mx[i][j+1]=max(mx[i][j+1],nk); } } } if (i>sti && j>stj) { if (++res[best]>=md) res[best]-=md; } } } for (int i=1; i<=n+m; i++) printf("%d%c",res[i],(i==n+m)?'\n':' '); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; const int MX=400,md=1000000007; short cur,f[MX][MX][MX+MX][2][2],mx[MX][MX]; int n,m,a[MX],b[MX],res[MX+MX]; void upd(short& x, short v) { if (v<x) x=v; } int main() { scanf("%d%d",&n,&m); for (int i=0; i<n; i++) scanf("%d",&a[i]); for (int i=0; i<m; i++) scanf("%d",&b[i]); for (int sti=0; sti<n; sti++) for (int stj=0; stj<m; stj++) { for (int i=sti; i<=n; i++) for (int j=stj; j<=m; j++) { memset(f[i][j],126,sizeof(f[i][j])); mx[i][j]=0; } int i=sti,j=stj; short inf=f[i][j][0][0][0]; f[i+1][j+1][1][int(a[i]>b[j])][0]=2; f[i+1][j+1][1][int(a[i]<b[j])][1]=2; mx[i+1][j+1]=1; if (i+1<n) { f[i+2][j][1][int(a[i+1]>a[i])][0]=2; mx[i+2][j]=1; } if (j+1<m) { f[i][j+2][1][int(b[j+1]>b[j])][1]=2; mx[i][j+2]=1; } for (int i=sti; i<=n; i++) for (int j=stj; j<=m; j++) { short best=n+m; for (int k=0; k<=mx[i][j]; k++) for (int up=0; up<2; up++) { short cur=f[i][j][k][up][0]; if (cur<inf) { best=min(best,cur); if (i<n) { short nup=(a[i]>a[i-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i+1][j][nk][nup][0],len); mx[i+1][j]=max(mx[i+1][j],nk); } if (j<m) { short nup=(b[j]>a[i-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i][j+1][nk][nup][1],len); mx[i][j+1]=max(mx[i][j+1],nk); } } cur=f[i][j][k][up][1]; if (cur<inf) { best=min(best,cur); if (i<n) { short nup=(a[i]>b[j-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i+1][j][nk][nup][0],len); mx[i+1][j]=max(mx[i+1][j],nk); } if (j<m) { short nup=(b[j]>b[j-1]); short nk=k+short(nup==up); short len=nk+1; if (cur>len) len=cur; upd(f[i][j+1][nk][nup][1],len); mx[i][j+1]=max(mx[i][j+1],nk); } } } if (i>sti && j>stj) { if (++res[best]>=md) res[best]-=md; } } } for (int i=1; i<=n+m; i++) printf("%d%c",res[i],(i==n+m)?'\n':' '); return 0; } |