Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- using namespace std;
- const int maxn = 100005;
- int n;
- int A[maxn], B[maxn];
- int dp[maxn][2];
- int rec(int at, int where) {
- if(at >= n) {
- return 0;
- }
- if(dp[at][where] != -1) {
- return dp[at][where];
- }
- int res = 0;
- if(where == 0 and at + 1 < n) {
- if(A[at + 1] >= A[at]) {
- res = max(res, rec(at + 1, where) + 1);
- }
- if(B[at + 1] >= A[at]) {
- res = max(res, rec(at + 1, 1) + 1);
- }
- }
- else if(where == 1 and at + 1 < n) {
- if(A[at + 1] >= B[at]) {
- res = max(res, rec(at + 1, 0) + 1);
- }
- if(B[at + 1] >= B[at]) {
- res = max(res, rec(at + 1, where) + 1);
- }
- }
- dp[at][where] = res;
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- memset(dp, -1, sizeof dp);
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> A[i];
- }
- for(int i = 0; i < n; i++) {
- cin >> B[i];
- }
- int res = 0;
- for(int i = 0; i < n; i++) {
- res = max(res, rec(i, 0) + 1);
- res = max(res, rec(i, 1) + 1);
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement