#include <iostream> #include <vector> #include <algorithm> /* #include "../../simple-console-debug/debug.h" */ #define MINN(x, y) x = min((x), (y)) using namespace std; const int MOD = 1000000007; const int INF = 10000000; struct DP { int aplus; int aminus; int bplus; int bminus; int a() const {return min(aplus, aminus);} int b() const {return min(bplus, bminus);} int all() const {return min(a(), b());} friend ostream& operator<<(ostream& os, const DP& dp) { return os << "(aplus=" << dp.aplus << " aminus=" << dp.aminus << " bplus=" << dp.bplus << " bminus=" << dp.bminus << ")"; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> A(n), B(m); for (int &x : A) cin >> x; for (int &x : B) cin >> x; /* DP dp[n+1][n+1][m+1][m+1][n+m+1]; */ DP *****dp = new DP****[n+1]; for (int i=0; i<n+1; i++) { dp[i] = new DP***[n+1]; for (int j=0; j<n+1; j++) { dp[i][j] = new DP**[m+1]; for (int k=0; k<m+1; k++) { dp[i][j][k] = new DP*[m+1]; for (int l=0; l<m+1; l++) dp[i][j][k][l] = new DP[n+m+1]; } } } /* vector<vector<vector<vector<vector<DP>>>>>dp(n+1, vector<vector<vector<vector<DP>>>>(n+1, vector<vector<vector<DP>>>(m+1, vector<vector<DP>>(m+1, vector<DP>(n+m+1))))); */ /* auto show_result = [&](int ai, int aj, int bi, int bj, int s) { */ /* cerr << deb::Container(vector<int>(A.begin()+ai, A.begin()+aj)); */ /* cerr << " X "; */ /* cerr << deb::Container(vector<int>(B.begin()+bi, B.begin()+bj)); */ /* cerr << " (" << s << ") -> " << dp[ai][aj][bi][bj][s] << endl; */ /* }; */ // init conditions auto init_helper = [&](const vector<int>& V, int i, int j) -> pair<int,int> { int max_block=2, this_block=2; for (int it=i+2; it<j; it++) if ((V[it-2] < V[it-1]) == (V[it-1] < V[it])) max_block = max(max_block, ++this_block); else this_block = 2; return {max_block, this_block}; }; for (int a=0; a<=n; a++) for (int bi=0; bi<m; bi++) { // solve len=1 separately dp[a][a][bi][bi+1][1] = {INF, INF, 1, 1}; // solve len>1 for (int bj=bi+2; bj<=m; bj++) { for (int s=1; s<=(bj-bi); s++) dp[a][a][bi][bj][s] = {INF, INF, INF, INF}; auto [val,suf] = init_helper(B, bi, bj); if (B[bj-2] < B[bj-1]) { dp[a][a][bi][bj][suf].bplus = val; dp[a][a][bi][bj][1].bminus = val; } else { dp[a][a][bi][bj][suf].bminus = val; dp[a][a][bi][bj][1].bplus = val; } /* for (int s=1; s<=(bj-bi); s++) */ /* show_result(a,a,bi,bj,s); */ } } for (int b=0; b<=m; b++) for (int ai=0; ai<n; ai++) { // solve len=1 separately dp[ai][ai+1][b][b][1] = {1, 1, INF, INF}; // solve len>1 for (int aj=ai+2; aj<=n; aj++) { for (int s=1; s<=(aj-ai); s++) dp[ai][aj][b][b][s] = {INF, INF, INF, INF}; auto [val,suf] = init_helper(A, ai, aj); if (A[aj-2] < A[aj-1]) { dp[ai][aj][b][b][suf].aplus = val; dp[ai][aj][b][b][1].aminus = val; } else { dp[ai][aj][b][b][suf].aminus = val; dp[ai][aj][b][b][1].aplus = val; } /* for (int s=1; s<=(aj-ai); s++) */ /* show_result(ai,aj,b,b,s); */ } } /* cerr << endl; */ /* cerr << endl; */ vector<long long> Results(n+m+1); for (int ai=0; ai<n; ai++) for (int aj=ai+1; aj<=n; aj++) for (int bi=0; bi<m; bi++) for (int bj=bi+1; bj<=m; bj++) { // solve s=1 separately dp[ai][aj][bi][bj][1] = {INF, INF, INF, INF}; /* //aplus */ /* if (aj>ai+1 && A[aj-2] > A[aj-1]) { */ /* cerr << "x" << endl; */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aplus, max(2, dp[ai][aj-1][bi][bj][ss].a())); */ /* } */ /* if (B[bj-1] > A[aj-1]) { */ /* cerr << "x" << endl; */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aplus, max(2, dp[ai][aj-1][bi][bj][ss].b())); */ /* } */ /* */ /* //bplus */ /* if (A[aj-1] > B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bplus, max(2, dp[ai][aj][bi][bj-1][ss].a())); */ /* if (bj>bi+1 && B[bj-2] > B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bplus, max(2, dp[ai][aj][bi][bj-1][ss].b())); */ /* */ /* //aminus */ /* if (aj>ai+1 && A[aj-2] < A[aj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aminus, max(2, dp[ai][aj-1][bi][bj][ss].a())); */ /* if (B[bj-1] < A[aj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aminus, max(2, dp[ai][aj-1][bi][bj][ss].b())); */ /* */ /* //bminus */ /* if (A[aj-1] < B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bminus, max(2, dp[ai][aj][bi][bj-1][ss].a())); */ /* if (bj>bi+1 && B[bj-2] < B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bminus, max(2, dp[ai][aj][bi][bj-1][ss].b())); */ /* */ /* show_result(ai,aj,bi,bj,1); */ int result = dp[ai][aj][bi][bj][1].all(); // solve s>1 for (int s=2; s<=(aj+bj-ai-bi); s++) { dp[ai][aj][bi][bj][s] = {INF, INF, INF, INF}; // aplus if (aj>ai+1 && A[aj-2] < A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aplus, max(s, dp[ai][aj-1][bi][bj][s-1].aplus)); if (B[bj-1] < A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aplus, max(s, dp[ai][aj-1][bi][bj][s-1].bplus)); //bplus if (A[aj-1] < B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bplus, max(s, dp[ai][aj][bi][bj-1][s-1].aplus)); if (bj>bi+1 && B[bj-2] < B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bplus, max(s, dp[ai][aj][bi][bj-1][s-1].bplus)); //aminus if (aj>ai+1 && A[aj-2] > A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aminus, max(s, dp[ai][aj-1][bi][bj][s-1].aminus)); if (B[bj-1] > A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aminus, max(s, dp[ai][aj-1][bi][bj][s-1].bminus)); //bminus if (A[aj-1] > B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bminus, max(s, dp[ai][aj][bi][bj-1][s-1].aminus)); if (bj>bi+1 && B[bj-2] > B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bminus, max(s, dp[ai][aj][bi][bj-1][s-1].bminus)); //s=1 MINN(dp[ai][aj][bi][bj][1].aplus, dp[ai][aj][bi][bj][s].aminus); MINN(dp[ai][aj][bi][bj][1].aminus, dp[ai][aj][bi][bj][s].aplus); MINN(dp[ai][aj][bi][bj][1].bplus, dp[ai][aj][bi][bj][s].bminus); MINN(dp[ai][aj][bi][bj][1].bminus, dp[ai][aj][bi][bj][s].bplus); MINN(result, dp[ai][aj][bi][bj][s].all()); /* show_result(ai,aj,bi,bj,s); */ } /* show_result(ai,aj,bi,bj,1); */ Results[result]++; } // output for (int i=1; i<=n+m; i++) cout << Results[i]%MOD << ' '; cout << '\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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | #include <iostream> #include <vector> #include <algorithm> /* #include "../../simple-console-debug/debug.h" */ #define MINN(x, y) x = min((x), (y)) using namespace std; const int MOD = 1000000007; const int INF = 10000000; struct DP { int aplus; int aminus; int bplus; int bminus; int a() const {return min(aplus, aminus);} int b() const {return min(bplus, bminus);} int all() const {return min(a(), b());} friend ostream& operator<<(ostream& os, const DP& dp) { return os << "(aplus=" << dp.aplus << " aminus=" << dp.aminus << " bplus=" << dp.bplus << " bminus=" << dp.bminus << ")"; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> A(n), B(m); for (int &x : A) cin >> x; for (int &x : B) cin >> x; /* DP dp[n+1][n+1][m+1][m+1][n+m+1]; */ DP *****dp = new DP****[n+1]; for (int i=0; i<n+1; i++) { dp[i] = new DP***[n+1]; for (int j=0; j<n+1; j++) { dp[i][j] = new DP**[m+1]; for (int k=0; k<m+1; k++) { dp[i][j][k] = new DP*[m+1]; for (int l=0; l<m+1; l++) dp[i][j][k][l] = new DP[n+m+1]; } } } /* vector<vector<vector<vector<vector<DP>>>>>dp(n+1, vector<vector<vector<vector<DP>>>>(n+1, vector<vector<vector<DP>>>(m+1, vector<vector<DP>>(m+1, vector<DP>(n+m+1))))); */ /* auto show_result = [&](int ai, int aj, int bi, int bj, int s) { */ /* cerr << deb::Container(vector<int>(A.begin()+ai, A.begin()+aj)); */ /* cerr << " X "; */ /* cerr << deb::Container(vector<int>(B.begin()+bi, B.begin()+bj)); */ /* cerr << " (" << s << ") -> " << dp[ai][aj][bi][bj][s] << endl; */ /* }; */ // init conditions auto init_helper = [&](const vector<int>& V, int i, int j) -> pair<int,int> { int max_block=2, this_block=2; for (int it=i+2; it<j; it++) if ((V[it-2] < V[it-1]) == (V[it-1] < V[it])) max_block = max(max_block, ++this_block); else this_block = 2; return {max_block, this_block}; }; for (int a=0; a<=n; a++) for (int bi=0; bi<m; bi++) { // solve len=1 separately dp[a][a][bi][bi+1][1] = {INF, INF, 1, 1}; // solve len>1 for (int bj=bi+2; bj<=m; bj++) { for (int s=1; s<=(bj-bi); s++) dp[a][a][bi][bj][s] = {INF, INF, INF, INF}; auto [val,suf] = init_helper(B, bi, bj); if (B[bj-2] < B[bj-1]) { dp[a][a][bi][bj][suf].bplus = val; dp[a][a][bi][bj][1].bminus = val; } else { dp[a][a][bi][bj][suf].bminus = val; dp[a][a][bi][bj][1].bplus = val; } /* for (int s=1; s<=(bj-bi); s++) */ /* show_result(a,a,bi,bj,s); */ } } for (int b=0; b<=m; b++) for (int ai=0; ai<n; ai++) { // solve len=1 separately dp[ai][ai+1][b][b][1] = {1, 1, INF, INF}; // solve len>1 for (int aj=ai+2; aj<=n; aj++) { for (int s=1; s<=(aj-ai); s++) dp[ai][aj][b][b][s] = {INF, INF, INF, INF}; auto [val,suf] = init_helper(A, ai, aj); if (A[aj-2] < A[aj-1]) { dp[ai][aj][b][b][suf].aplus = val; dp[ai][aj][b][b][1].aminus = val; } else { dp[ai][aj][b][b][suf].aminus = val; dp[ai][aj][b][b][1].aplus = val; } /* for (int s=1; s<=(aj-ai); s++) */ /* show_result(ai,aj,b,b,s); */ } } /* cerr << endl; */ /* cerr << endl; */ vector<long long> Results(n+m+1); for (int ai=0; ai<n; ai++) for (int aj=ai+1; aj<=n; aj++) for (int bi=0; bi<m; bi++) for (int bj=bi+1; bj<=m; bj++) { // solve s=1 separately dp[ai][aj][bi][bj][1] = {INF, INF, INF, INF}; /* //aplus */ /* if (aj>ai+1 && A[aj-2] > A[aj-1]) { */ /* cerr << "x" << endl; */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aplus, max(2, dp[ai][aj-1][bi][bj][ss].a())); */ /* } */ /* if (B[bj-1] > A[aj-1]) { */ /* cerr << "x" << endl; */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aplus, max(2, dp[ai][aj-1][bi][bj][ss].b())); */ /* } */ /* */ /* //bplus */ /* if (A[aj-1] > B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bplus, max(2, dp[ai][aj][bi][bj-1][ss].a())); */ /* if (bj>bi+1 && B[bj-2] > B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bplus, max(2, dp[ai][aj][bi][bj-1][ss].b())); */ /* */ /* //aminus */ /* if (aj>ai+1 && A[aj-2] < A[aj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aminus, max(2, dp[ai][aj-1][bi][bj][ss].a())); */ /* if (B[bj-1] < A[aj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].aminus, max(2, dp[ai][aj-1][bi][bj][ss].b())); */ /* */ /* //bminus */ /* if (A[aj-1] < B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bminus, max(2, dp[ai][aj][bi][bj-1][ss].a())); */ /* if (bj>bi+1 && B[bj-2] < B[bj-1]) */ /* for (int ss=1; ss<(aj+bj-ai-bi); ss++) */ /* MINN(dp[ai][aj][bi][bj][1].bminus, max(2, dp[ai][aj][bi][bj-1][ss].b())); */ /* */ /* show_result(ai,aj,bi,bj,1); */ int result = dp[ai][aj][bi][bj][1].all(); // solve s>1 for (int s=2; s<=(aj+bj-ai-bi); s++) { dp[ai][aj][bi][bj][s] = {INF, INF, INF, INF}; // aplus if (aj>ai+1 && A[aj-2] < A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aplus, max(s, dp[ai][aj-1][bi][bj][s-1].aplus)); if (B[bj-1] < A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aplus, max(s, dp[ai][aj-1][bi][bj][s-1].bplus)); //bplus if (A[aj-1] < B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bplus, max(s, dp[ai][aj][bi][bj-1][s-1].aplus)); if (bj>bi+1 && B[bj-2] < B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bplus, max(s, dp[ai][aj][bi][bj-1][s-1].bplus)); //aminus if (aj>ai+1 && A[aj-2] > A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aminus, max(s, dp[ai][aj-1][bi][bj][s-1].aminus)); if (B[bj-1] > A[aj-1]) MINN(dp[ai][aj][bi][bj][s].aminus, max(s, dp[ai][aj-1][bi][bj][s-1].bminus)); //bminus if (A[aj-1] > B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bminus, max(s, dp[ai][aj][bi][bj-1][s-1].aminus)); if (bj>bi+1 && B[bj-2] > B[bj-1]) MINN(dp[ai][aj][bi][bj][s].bminus, max(s, dp[ai][aj][bi][bj-1][s-1].bminus)); //s=1 MINN(dp[ai][aj][bi][bj][1].aplus, dp[ai][aj][bi][bj][s].aminus); MINN(dp[ai][aj][bi][bj][1].aminus, dp[ai][aj][bi][bj][s].aplus); MINN(dp[ai][aj][bi][bj][1].bplus, dp[ai][aj][bi][bj][s].bminus); MINN(dp[ai][aj][bi][bj][1].bminus, dp[ai][aj][bi][bj][s].bplus); MINN(result, dp[ai][aj][bi][bj][s].all()); /* show_result(ai,aj,bi,bj,s); */ } /* show_result(ai,aj,bi,bj,1); */ Results[result]++; } // output for (int i=1; i<=n+m; i++) cout << Results[i]%MOD << ' '; cout << '\n'; return 0; } |