| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1281418 | Kerim | Exam (eJOI20_exam) | C++17 | 43 ms | 98416 KiB |
#include "bits/stdc++.h"
using namespace std;
const int N = 1e5+5;
const int K = 5005;
int l[N], r[N], a[N], b[N];
int dp[K][K], 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);
}
if (n < K){
memset(dp, -1, sizeof dp);
printf("%d\n", rec(0, 0));
}
else if (count(b+1, b+n+1, b[1]) == n){//subtask 2
int found = 0, cnt = 0, answer = 0;
for (int i = 1; i <= n; i++){
if (a[i] <= b[i]){
found |= a[i] == b[i];
cnt += 1;
}
else{
if (found)
answer += cnt;
cnt = found = 0;
}
}
if (found)
answer += cnt;
printf("%d\n", answer);
}
else
assert(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... | ||||
