//ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define mod 1000000007 #define F first #define S second #define ll int #define N 110 using namespace std; ll n,m,a[N],b[N],dp[N][N][N][2][2],ans[N]; void solve(ll x,ll y) { ll lim=max(n-x,m-y)+3,i,j,k; for(i=x;i<=n;i++) { for(j=y;j<=m;j++) { for(k=2;k<=lim;k++) { dp[i][j][k][0][0]=dp[i][j][k][0][1]=dp[i][j][k][1][0]=dp[i][j][k][1][1]=INF; } } } dp[x+2][y][2][0][a[x+1]>a[x]]=2; dp[x+1][y+1][2][0][a[x]>b[y]]=2; dp[x+1][y+1][2][1][b[y]>a[x]]=2; dp[x][y+2][2][1][b[y+1]>b[y]]=2; for(i=x;i<=n;i++) { for(j=y;j<=m;j++) { for(k=2;k<=lim;k++) { if(dp[i][j][k][0][0]<INF) { ll v=(a[i]>a[i-1]?2:dp[i][j][k][0][0]+1); dp[i+1][j][max(k,v)][0][a[i]>a[i-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>a[i-1]],v); v=(b[j]>a[i-1]?2:dp[i][j][k][0][0]+1); dp[i][j+1][max(k,v)][1][b[j]>a[i-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>a[i-1]],v); } if(dp[i][j][k][0][1]<INF) { ll v=(a[i]<a[i-1]?2:dp[i][j][k][0][1]+1); dp[i+1][j][max(k,v)][0][a[i]>a[i-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>a[i-1]],v); v=(b[j]<a[i-1]?2:dp[i][j][k][0][1]+1); dp[i][j+1][max(k,v)][1][b[j]>a[i-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>a[i-1]],v); } if(dp[i][j][k][1][0]<INF) { ll v=(a[i]>b[j-1]?2:dp[i][j][k][1][0]+1); dp[i+1][j][max(k,v)][0][a[i]>b[j-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>b[j-1]],v); v=(b[j]>b[j-1]?2:dp[i][j][k][1][0]+1); dp[i][j+1][max(k,v)][1][b[j]>b[j-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>b[j-1]],v); } if(dp[i][j][k][1][1]<INF) { ll v=(a[i]<b[j-1]?2:dp[i][j][k][1][1]+1); dp[i+1][j][max(k,v)][0][a[i]>b[j-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>b[j-1]],v); v=(b[j]<b[j-1]?2:dp[i][j][k][1][1]+1); dp[i][j+1][max(k,v)][1][b[j]>b[j-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>b[j-1]],v); } } } } for(i=x;i<n;i++) { for(j=y;j<m;j++) { for(k=2;k<=lim;k++) { if(dp[i+1][j+1][k][0][0]<INF||dp[i+1][j+1][k][0][1]<INF||dp[i+1][j+1][k][1][0]<INF||dp[i+1][j+1][k][1][1]<INF) { break; } } ans[k]++; } } return; } int main(){ ll i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<m;i++) { scanf("%d",&b[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { solve(i,j); } } for(i=1;i<=n+m;i++) { printf("%d ",ans[i]); } puts(""); 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | //ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define mod 1000000007 #define F first #define S second #define ll int #define N 110 using namespace std; ll n,m,a[N],b[N],dp[N][N][N][2][2],ans[N]; void solve(ll x,ll y) { ll lim=max(n-x,m-y)+3,i,j,k; for(i=x;i<=n;i++) { for(j=y;j<=m;j++) { for(k=2;k<=lim;k++) { dp[i][j][k][0][0]=dp[i][j][k][0][1]=dp[i][j][k][1][0]=dp[i][j][k][1][1]=INF; } } } dp[x+2][y][2][0][a[x+1]>a[x]]=2; dp[x+1][y+1][2][0][a[x]>b[y]]=2; dp[x+1][y+1][2][1][b[y]>a[x]]=2; dp[x][y+2][2][1][b[y+1]>b[y]]=2; for(i=x;i<=n;i++) { for(j=y;j<=m;j++) { for(k=2;k<=lim;k++) { if(dp[i][j][k][0][0]<INF) { ll v=(a[i]>a[i-1]?2:dp[i][j][k][0][0]+1); dp[i+1][j][max(k,v)][0][a[i]>a[i-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>a[i-1]],v); v=(b[j]>a[i-1]?2:dp[i][j][k][0][0]+1); dp[i][j+1][max(k,v)][1][b[j]>a[i-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>a[i-1]],v); } if(dp[i][j][k][0][1]<INF) { ll v=(a[i]<a[i-1]?2:dp[i][j][k][0][1]+1); dp[i+1][j][max(k,v)][0][a[i]>a[i-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>a[i-1]],v); v=(b[j]<a[i-1]?2:dp[i][j][k][0][1]+1); dp[i][j+1][max(k,v)][1][b[j]>a[i-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>a[i-1]],v); } if(dp[i][j][k][1][0]<INF) { ll v=(a[i]>b[j-1]?2:dp[i][j][k][1][0]+1); dp[i+1][j][max(k,v)][0][a[i]>b[j-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>b[j-1]],v); v=(b[j]>b[j-1]?2:dp[i][j][k][1][0]+1); dp[i][j+1][max(k,v)][1][b[j]>b[j-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>b[j-1]],v); } if(dp[i][j][k][1][1]<INF) { ll v=(a[i]<b[j-1]?2:dp[i][j][k][1][1]+1); dp[i+1][j][max(k,v)][0][a[i]>b[j-1]]=min(dp[i+1][j][max(k,v)][0][a[i]>b[j-1]],v); v=(b[j]<b[j-1]?2:dp[i][j][k][1][1]+1); dp[i][j+1][max(k,v)][1][b[j]>b[j-1]]=min(dp[i][j+1][max(k,v)][1][b[j]>b[j-1]],v); } } } } for(i=x;i<n;i++) { for(j=y;j<m;j++) { for(k=2;k<=lim;k++) { if(dp[i+1][j+1][k][0][0]<INF||dp[i+1][j+1][k][0][1]<INF||dp[i+1][j+1][k][1][0]<INF||dp[i+1][j+1][k][1][1]<INF) { break; } } ans[k]++; } } return; } int main(){ ll i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<m;i++) { scanf("%d",&b[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { solve(i,j); } } for(i=1;i<=n+m;i++) { printf("%d ",ans[i]); } puts(""); return 0; } |