# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
809080 |
2023-08-05T15:49:28 Z |
AndrijaM |
Exam (eJOI20_exam) |
C++14 |
|
228 ms |
99572 KB |
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int INF = 2e9;
int n;
int a[maxn], b[maxn];
int L[maxn], R[maxn];
int segment_tree[4 * maxn];
void solve_subtask2() {
int x = b[0];
int res = 0;
for(int i = 0; i < n; i++) {
if(a[i] == x) {
res++;
int j = i - 1;
while(j >= 0 and a[j] <= x) {
j--;
res++;
}
j = i + 1;
while(j < n and a[j] <= x) {
res++;
j++;
}
i = j - 1;
}
}
cout << res << endl;
}
int dp[5005][5005];
int rec(int i, int j) {
if(i < 0 or j < 0) {
return 0;
}
if(dp[i][j] != -1) {
return dp[i][j];
}
int res = 0;
if(a[i] == b[j] and L[i] < j and R[i] > j) {
res = max(res, rec(i, j - 1) + 1);
}
res = max(res, rec(i - 1, j));
res = max(res, rec(i, j - 1));
return dp[i][j] = res;
}
void build_tree(int L, int R, int node) {
if(L == R) {
segment_tree[node] = 0;
return;
}
int middle = (L + R) / 2;
build_tree(L, middle, 2 * node);
build_tree(middle + 1, R, 2 * node + 1);
segment_tree[node] = max(segment_tree[2 * node], segment_tree[2 * node + 1]);
}
int query(int i, int j, int L, int R, int node) {
// L R i L R j L R
if(i <= L and R <= j) {
return segment_tree[node];
}
if(R < i or j < L) {
return 0;
}
int middle = (L + R) / 2;
return max(query(i, j, L, middle, 2 * node), query(i, j, middle + 1, R, 2 * node + 1));
}
void update(int idx, int new_value, int L, int R, int node) {
if(L == R) {
segment_tree[node] = new_value;
return;
}
int middle = (L + R) / 2;
if(idx <= middle) {
update(idx, new_value, L, middle, 2 * node);
}
else {
update(idx, new_value, middle + 1, R, 2 * node + 1);
}
segment_tree[node] = max(segment_tree[2 * node], segment_tree[2 * node + 1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
set<int> st;
vector<int> t(n + 5);
for(int i = 0; i < n; i++) {
cin >> a[i];
t[i + 1] = a[i];
}
for(int i = 0; i < n; i++) {
cin >> b[i];
st.insert(b[i]);
}
t[0] = INF;
t[n + 1] = INF;
stack<int> s;
s.push(0);
for(int i = 1; i <= n; i++) {
while(t[s.top()] <= t[i]) {
s.pop();
}
L[i - 1] = s.top() - 1;
s.push(i);
}
while(!s.empty()) {
s.pop();
}
s.push(n + 1);
for(int i = n; i > 0; i--) {
while(t[s.top()] <= t[i]) {
s.pop();
}
R[i - 1] = s.top() - 1;
s.push(i);
}
if((int) st.size() == 1) {
solve_subtask2();
return 0;
}
else if(n <= 5005){
memset(dp, -1, sizeof dp);
cout << rec(n - 1, n - 1) << endl;
}
else {
build_tree(0, n - 1, 1);
map<int, int> m;
for(int i = 0; i < n; i++) {
m[a[i]] = i;
}
for(int i = 0; i < n; i++) {
map<int, int>::iterator it = m.find(b[i]);
if(it != m.end()) {
int idx = it->second;
if(L[idx] < i and i < R[idx]) {
int dp = query(0, idx, 0, n - 1, 1);
update(idx, dp + 1, 0, n - 1, 1);
}
}
}
cout << query(0, n - 1, 0, n - 1, 1) << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98252 KB |
Output is correct |
2 |
Correct |
35 ms |
98296 KB |
Output is correct |
3 |
Correct |
34 ms |
98368 KB |
Output is correct |
4 |
Correct |
32 ms |
98352 KB |
Output is correct |
5 |
Correct |
33 ms |
98252 KB |
Output is correct |
6 |
Correct |
33 ms |
98324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
6 ms |
1108 KB |
Output is correct |
3 |
Correct |
13 ms |
3608 KB |
Output is correct |
4 |
Correct |
10 ms |
2592 KB |
Output is correct |
5 |
Correct |
20 ms |
4308 KB |
Output is correct |
6 |
Correct |
12 ms |
2644 KB |
Output is correct |
7 |
Correct |
13 ms |
2808 KB |
Output is correct |
8 |
Correct |
19 ms |
4168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
98292 KB |
Output is correct |
2 |
Correct |
35 ms |
98396 KB |
Output is correct |
3 |
Correct |
61 ms |
98860 KB |
Output is correct |
4 |
Correct |
169 ms |
99392 KB |
Output is correct |
5 |
Correct |
157 ms |
99464 KB |
Output is correct |
6 |
Correct |
164 ms |
99520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
99496 KB |
Output is correct |
2 |
Correct |
28 ms |
5392 KB |
Output is correct |
3 |
Correct |
80 ms |
12136 KB |
Output is correct |
4 |
Correct |
82 ms |
12912 KB |
Output is correct |
5 |
Correct |
95 ms |
12240 KB |
Output is correct |
6 |
Correct |
88 ms |
11536 KB |
Output is correct |
7 |
Correct |
83 ms |
12492 KB |
Output is correct |
8 |
Correct |
72 ms |
11532 KB |
Output is correct |
9 |
Correct |
86 ms |
12292 KB |
Output is correct |
10 |
Correct |
64 ms |
11944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98252 KB |
Output is correct |
2 |
Correct |
35 ms |
98296 KB |
Output is correct |
3 |
Correct |
34 ms |
98368 KB |
Output is correct |
4 |
Correct |
32 ms |
98352 KB |
Output is correct |
5 |
Correct |
33 ms |
98252 KB |
Output is correct |
6 |
Correct |
33 ms |
98324 KB |
Output is correct |
7 |
Correct |
34 ms |
98308 KB |
Output is correct |
8 |
Correct |
35 ms |
98364 KB |
Output is correct |
9 |
Correct |
37 ms |
98380 KB |
Output is correct |
10 |
Correct |
35 ms |
98388 KB |
Output is correct |
11 |
Correct |
35 ms |
98340 KB |
Output is correct |
12 |
Correct |
34 ms |
98376 KB |
Output is correct |
13 |
Correct |
34 ms |
98372 KB |
Output is correct |
14 |
Correct |
34 ms |
98332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98252 KB |
Output is correct |
2 |
Correct |
35 ms |
98296 KB |
Output is correct |
3 |
Correct |
34 ms |
98368 KB |
Output is correct |
4 |
Correct |
32 ms |
98352 KB |
Output is correct |
5 |
Correct |
33 ms |
98252 KB |
Output is correct |
6 |
Correct |
33 ms |
98324 KB |
Output is correct |
7 |
Correct |
34 ms |
98292 KB |
Output is correct |
8 |
Correct |
35 ms |
98396 KB |
Output is correct |
9 |
Correct |
61 ms |
98860 KB |
Output is correct |
10 |
Correct |
169 ms |
99392 KB |
Output is correct |
11 |
Correct |
157 ms |
99464 KB |
Output is correct |
12 |
Correct |
164 ms |
99520 KB |
Output is correct |
13 |
Correct |
34 ms |
98308 KB |
Output is correct |
14 |
Correct |
35 ms |
98364 KB |
Output is correct |
15 |
Correct |
37 ms |
98380 KB |
Output is correct |
16 |
Correct |
35 ms |
98388 KB |
Output is correct |
17 |
Correct |
35 ms |
98340 KB |
Output is correct |
18 |
Correct |
34 ms |
98376 KB |
Output is correct |
19 |
Correct |
34 ms |
98372 KB |
Output is correct |
20 |
Correct |
34 ms |
98332 KB |
Output is correct |
21 |
Correct |
34 ms |
98348 KB |
Output is correct |
22 |
Correct |
40 ms |
98508 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
228 ms |
99340 KB |
Output is correct |
25 |
Correct |
180 ms |
99352 KB |
Output is correct |
26 |
Correct |
157 ms |
99292 KB |
Output is correct |
27 |
Correct |
163 ms |
99168 KB |
Output is correct |
28 |
Correct |
156 ms |
99572 KB |
Output is correct |
29 |
Correct |
156 ms |
99428 KB |
Output is correct |
30 |
Correct |
158 ms |
99480 KB |
Output is correct |
31 |
Correct |
177 ms |
99312 KB |
Output is correct |
32 |
Correct |
157 ms |
99476 KB |
Output is correct |