| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1281416 | Kerim | Exam (eJOI20_exam) | C++17 | 721 ms | 99332 KiB |
#include "bits/stdc++.h"
using namespace std;
const int N = 5005;
int l[N], r[N], a[N], b[N];
int dp[N][N], n;
int rec(int x, int y){
if (x > n or y > n)
return 0;
int &ret = dp[x][y];
if (~ret)
return ret;
ret = max(rec(x+1, y), rec(x, y+1)); //skip
if (a[x] == b[y] and l[x] <= y and y <= r[x]) //match
ret = max(ret, rec(x, y+1) + 1);
return ret;
}
int main(){
// freopen("file.in", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", a+i);
for (int i = 1; i <= n; i++)
scanf("%d", b+i);
a[0] = a[n+1] = 2e9;
stack<int>st;
//for left
st.push(0);
for (int i = 1; i <= n; i++){
while (!st.empty() and a[st.top()] <= a[i])
st.pop();
l[i] = st.top()+1;
st.push(i);
}
//for right
while (!st.empty())
st.pop();
st.push(n+1);
for (int i = n; i >= 1; i--){
while (!st.empty() and a[st.top()] <= a[i])
st.pop();
r[i] = st.top()-1;
st.push(i);
}
memset(dp, -1, sizeof dp);
printf("%d\n", rec(0, 0));
}
Compilation message (stderr)
| # | 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... | ||||
