# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
813509 |
2023-08-07T19:16:56 Z |
Sputnik123 |
Exam (eJOI20_exam) |
C++14 |
|
224 ms |
99528 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 |
31 ms |
98252 KB |
Output is correct |
2 |
Correct |
31 ms |
98348 KB |
Output is correct |
3 |
Correct |
32 ms |
98356 KB |
Output is correct |
4 |
Correct |
32 ms |
98388 KB |
Output is correct |
5 |
Correct |
32 ms |
98256 KB |
Output is correct |
6 |
Correct |
32 ms |
98268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1076 KB |
Output is correct |
3 |
Correct |
15 ms |
3440 KB |
Output is correct |
4 |
Correct |
10 ms |
2652 KB |
Output is correct |
5 |
Correct |
20 ms |
4300 KB |
Output is correct |
6 |
Correct |
11 ms |
2644 KB |
Output is correct |
7 |
Correct |
14 ms |
2772 KB |
Output is correct |
8 |
Correct |
19 ms |
4220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
98388 KB |
Output is correct |
2 |
Correct |
34 ms |
98452 KB |
Output is correct |
3 |
Correct |
53 ms |
98780 KB |
Output is correct |
4 |
Correct |
145 ms |
99348 KB |
Output is correct |
5 |
Correct |
161 ms |
99356 KB |
Output is correct |
6 |
Correct |
163 ms |
99528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
99508 KB |
Output is correct |
2 |
Correct |
28 ms |
5448 KB |
Output is correct |
3 |
Correct |
78 ms |
12128 KB |
Output is correct |
4 |
Correct |
79 ms |
12880 KB |
Output is correct |
5 |
Correct |
91 ms |
12280 KB |
Output is correct |
6 |
Correct |
86 ms |
11468 KB |
Output is correct |
7 |
Correct |
80 ms |
12428 KB |
Output is correct |
8 |
Correct |
77 ms |
11524 KB |
Output is correct |
9 |
Correct |
96 ms |
12296 KB |
Output is correct |
10 |
Correct |
63 ms |
12068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
98252 KB |
Output is correct |
2 |
Correct |
31 ms |
98348 KB |
Output is correct |
3 |
Correct |
32 ms |
98356 KB |
Output is correct |
4 |
Correct |
32 ms |
98388 KB |
Output is correct |
5 |
Correct |
32 ms |
98256 KB |
Output is correct |
6 |
Correct |
32 ms |
98268 KB |
Output is correct |
7 |
Correct |
39 ms |
98248 KB |
Output is correct |
8 |
Correct |
32 ms |
98360 KB |
Output is correct |
9 |
Correct |
30 ms |
98352 KB |
Output is correct |
10 |
Correct |
31 ms |
98400 KB |
Output is correct |
11 |
Correct |
30 ms |
98380 KB |
Output is correct |
12 |
Correct |
31 ms |
98376 KB |
Output is correct |
13 |
Correct |
30 ms |
98380 KB |
Output is correct |
14 |
Correct |
33 ms |
98332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
98252 KB |
Output is correct |
2 |
Correct |
31 ms |
98348 KB |
Output is correct |
3 |
Correct |
32 ms |
98356 KB |
Output is correct |
4 |
Correct |
32 ms |
98388 KB |
Output is correct |
5 |
Correct |
32 ms |
98256 KB |
Output is correct |
6 |
Correct |
32 ms |
98268 KB |
Output is correct |
7 |
Correct |
31 ms |
98388 KB |
Output is correct |
8 |
Correct |
34 ms |
98452 KB |
Output is correct |
9 |
Correct |
53 ms |
98780 KB |
Output is correct |
10 |
Correct |
145 ms |
99348 KB |
Output is correct |
11 |
Correct |
161 ms |
99356 KB |
Output is correct |
12 |
Correct |
163 ms |
99528 KB |
Output is correct |
13 |
Correct |
39 ms |
98248 KB |
Output is correct |
14 |
Correct |
32 ms |
98360 KB |
Output is correct |
15 |
Correct |
30 ms |
98352 KB |
Output is correct |
16 |
Correct |
31 ms |
98400 KB |
Output is correct |
17 |
Correct |
30 ms |
98380 KB |
Output is correct |
18 |
Correct |
31 ms |
98376 KB |
Output is correct |
19 |
Correct |
30 ms |
98380 KB |
Output is correct |
20 |
Correct |
33 ms |
98332 KB |
Output is correct |
21 |
Correct |
31 ms |
98400 KB |
Output is correct |
22 |
Correct |
45 ms |
98560 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
224 ms |
99348 KB |
Output is correct |
25 |
Correct |
174 ms |
99348 KB |
Output is correct |
26 |
Correct |
181 ms |
99288 KB |
Output is correct |
27 |
Correct |
180 ms |
99284 KB |
Output is correct |
28 |
Correct |
159 ms |
99464 KB |
Output is correct |
29 |
Correct |
172 ms |
99424 KB |
Output is correct |
30 |
Correct |
155 ms |
99404 KB |
Output is correct |
31 |
Correct |
184 ms |
99424 KB |
Output is correct |
32 |
Correct |
155 ms |
99480 KB |
Output is correct |