Submission #1281416

#TimeUsernameProblemLanguageResultExecution timeMemory
1281416KerimExam (eJOI20_exam)C++17
65 / 100
721 ms99332 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)

exam.cpp: In function 'int main()':
exam.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
exam.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         scanf("%d", a+i);
      |         ~~~~~^~~~~~~~~~~
exam.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         scanf("%d", b+i);
      |         ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...