#include <iostream> #include <limits> constexpr int max_n_m=600001; long long res[max_n_m]; void solve(int* a, int n, int* b, int mm){ //a[0...n-1], b[0...m-1] const int BASE=n+mm+1; const int DIM=BASE+BASE+1; int dp[n+1][mm+1][2][DIM]; for (int i=0; i<=n; ++i){ for (int j=0; j<=mm; ++j){ for (int k=0; k<2; ++k){ for (int m=0; m<DIM; ++m) dp[i][j][k][m]=std::numeric_limits<int>::max(); } } } dp[1][0][0][BASE]=1; dp[0][1][1][BASE]=1; for (int i=0; i<=n; ++i){ for (int j=0; j<=mm; ++j){ if (i==0 and j==0)continue; for (int k=0; k<2; ++k){ for (int m=0; m<DIM; ++m){ if (dp[i][j][k][m]!=std::numeric_limits<int>::max()){ int last; if (k==0){ last=a[i-1]; }else{ last=b[j-1]; } if (i!=n){ if (a[i]>last){//dodaje wieksze if (m>=BASE){//streak rosnacy dp[i+1][j][0][m+1]=std::min(dp[i+1][j][0][m+1],std::max(m-BASE+1+1,dp[i][j][k][m])); }else{//streak na baze dp[i+1][j][0][BASE+1]=std::min(dp[i+1][j][0][BASE+1],dp[i][j][k][m]); } }else{//dodaje mniejsze if (m>BASE){//streak rosnacy dp[i+1][j][0][BASE-1]=std::min(dp[i+1][j][0][BASE-1],dp[i][j][k][m]); }else{ dp[i+1][j][0][m-1]=std::min(dp[i+1][j][0][m-1],std::max(BASE-m+1+1,dp[i][j][k][m])); } } } if (j!=mm){ if (b[j]>last){//dodaje wieksze if (m>=BASE){//streak rosnacy dp[i][j+1][1][m+1]=std::min(dp[i][j+1][1][m+1],std::max(m-BASE+1+1,dp[i][j][k][m])); }else{//streak na baze dp[i][j+1][1][BASE+1]=std::min(dp[i][j+1][1][BASE+1],dp[i][j][k][m]); } }else{//dodaje mniejsze if (m>BASE){//streak rosnacy //if (i==0 and j+1==4)std::cout<<"TAK\n"; dp[i][j+1][1][BASE-1]=std::min(dp[i][j+1][1][BASE-1],dp[i][j][k][m]); }else{ dp[i][j+1][1][m-1]=std::min(dp[i][j+1][1][m-1],std::max(BASE-m+1+1,dp[i][j][k][m])); } } } } } } } } for (size_t i=1; i<=n; ++i){ for (size_t j=1; j<=mm; ++j){ int res=std::numeric_limits<int>::max(); for (size_t k=0; k<2; ++k){ for (size_t m=0; m<DIM; ++m){ res=std::min(res,dp[i][j][k][m]); } } ++::res[res]; } } } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, m; std::cin>>n>>m; int a[n], b[m]; for (size_t i=0; i<n; ++i)std::cin>>a[i]; for (size_t i=0; i<m; ++i)std::cin>>b[i]; for (size_t i=0; i<n; ++i){ for (size_t j=0; j<m; ++j) solve(a+i,n-i,b+j,m-j); } for (size_t i=1; i<=n+m; ++i)std::cout<<res[i]<<' '; std::cout<<'\n'; }
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 | #include <iostream> #include <limits> constexpr int max_n_m=600001; long long res[max_n_m]; void solve(int* a, int n, int* b, int mm){ //a[0...n-1], b[0...m-1] const int BASE=n+mm+1; const int DIM=BASE+BASE+1; int dp[n+1][mm+1][2][DIM]; for (int i=0; i<=n; ++i){ for (int j=0; j<=mm; ++j){ for (int k=0; k<2; ++k){ for (int m=0; m<DIM; ++m) dp[i][j][k][m]=std::numeric_limits<int>::max(); } } } dp[1][0][0][BASE]=1; dp[0][1][1][BASE]=1; for (int i=0; i<=n; ++i){ for (int j=0; j<=mm; ++j){ if (i==0 and j==0)continue; for (int k=0; k<2; ++k){ for (int m=0; m<DIM; ++m){ if (dp[i][j][k][m]!=std::numeric_limits<int>::max()){ int last; if (k==0){ last=a[i-1]; }else{ last=b[j-1]; } if (i!=n){ if (a[i]>last){//dodaje wieksze if (m>=BASE){//streak rosnacy dp[i+1][j][0][m+1]=std::min(dp[i+1][j][0][m+1],std::max(m-BASE+1+1,dp[i][j][k][m])); }else{//streak na baze dp[i+1][j][0][BASE+1]=std::min(dp[i+1][j][0][BASE+1],dp[i][j][k][m]); } }else{//dodaje mniejsze if (m>BASE){//streak rosnacy dp[i+1][j][0][BASE-1]=std::min(dp[i+1][j][0][BASE-1],dp[i][j][k][m]); }else{ dp[i+1][j][0][m-1]=std::min(dp[i+1][j][0][m-1],std::max(BASE-m+1+1,dp[i][j][k][m])); } } } if (j!=mm){ if (b[j]>last){//dodaje wieksze if (m>=BASE){//streak rosnacy dp[i][j+1][1][m+1]=std::min(dp[i][j+1][1][m+1],std::max(m-BASE+1+1,dp[i][j][k][m])); }else{//streak na baze dp[i][j+1][1][BASE+1]=std::min(dp[i][j+1][1][BASE+1],dp[i][j][k][m]); } }else{//dodaje mniejsze if (m>BASE){//streak rosnacy //if (i==0 and j+1==4)std::cout<<"TAK\n"; dp[i][j+1][1][BASE-1]=std::min(dp[i][j+1][1][BASE-1],dp[i][j][k][m]); }else{ dp[i][j+1][1][m-1]=std::min(dp[i][j+1][1][m-1],std::max(BASE-m+1+1,dp[i][j][k][m])); } } } } } } } } for (size_t i=1; i<=n; ++i){ for (size_t j=1; j<=mm; ++j){ int res=std::numeric_limits<int>::max(); for (size_t k=0; k<2; ++k){ for (size_t m=0; m<DIM; ++m){ res=std::min(res,dp[i][j][k][m]); } } ++::res[res]; } } } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, m; std::cin>>n>>m; int a[n], b[m]; for (size_t i=0; i<n; ++i)std::cin>>a[i]; for (size_t i=0; i<m; ++i)std::cin>>b[i]; for (size_t i=0; i<n; ++i){ for (size_t j=0; j<m; ++j) solve(a+i,n-i,b+j,m-j); } for (size_t i=1; i<=n+m; ++i)std::cout<<res[i]<<' '; std::cout<<'\n'; } |