This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define For(i, n) for (int i = 0; i < n; i++)
#define Each(x, v) for (auto x : v)
#define pb push_back
const int MAXN = 200;
int N, H[MAXN + 5], T[MAXN + 5], dp[MAXN + 5][MAXN + 5];
int f(int l, int r) {
if (l > r) return 0;
if (l == r) {
if (T[l] == H[l]) return 1;
return 0;
}
if (dp[l][r] != -1) return dp[l][r];
int terbesar = 0, cnt = 0;
for (int i = l; i <= r; i++) terbesar = max(terbesar, H[i]);
for (int i = l; i <= r; i++) {
if (T[i] == terbesar) {
cnt++;
}
}
int ans = cnt;
for (int i = l; i <= r - 1; i++) {
ans = max(ans, f(l, i) + f(i + 1, r));
}
return dp[l][r] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(dp, -1, sizeof dp);
cin >> N;
For (i, N) cin >> H[i];
For (i, N) cin >> T[i];
cout << f(0, N - 1) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |