This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
const int N = 1e6 + 5;
bool maxi(int &x, int y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int n;
int a[N], b[N];
int l[N], r[N];
namespace task2 {
bool check() {
for (int i = 1; i + 1 <= n; ++i) {
if (b[i] != b[i + 1]) return false;
}
return true;
}
int d[N];
void solve() {
for (int i = 1; i <= n; ++i) if (a[i] == b[1]) {
int x = l[i] + 1;
int y = r[i] - 1;
d[x]++;
d[y + 1]--;
}
for (int i = 1; i <= n; ++i) d[i] += d[i - 1];
int ans = 0;
for (int i = 1; i <= n; ++i) ans += d[i] != 0;
cout << ans;
}
}
namespace task4 {
bool check() {
set<int> s;
for (int i = 1; i <= n; ++i) s.insert(a[i]);
return (s.size() == n);
}
int bit[N];
void update(int u, int c) {
for (; u < N; u += u & -u) maxi(bit[u], c);
}
int get(int u) {
int res = 0;
for (; u > 0; u -= u & -u) maxi(res, bit[u]);
return res;
}
map<int, int> pos;
void solve() {
for (int i = 1; i <= n; ++i) pos[a[i]] = i;
for (int i = 1; i <= n; ++i) {
auto it = pos.find(b[i]);
if (it == pos.end()) continue;
int x = pos[b[i]];
if (l[x] < i && i < r[x]) {
int cur = get(i - 1) + 1;
update(i, cur);
}
}
cout << get(n);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
stack<int> st;
for (int i = 1; i <= n; ++i) {
while (st.size() && a[i] >= a[st.top()]) st.pop();
l[i] = st.size() ? st.top() : 0;
st.push(i);
}
st = stack<int> {};
for (int i = n; i >= 1; --i) {
while (st.size() && a[i] >= a[st.top()]) st.pop();
r[i] = st.size() ? st.top() : n + 1;
st.push(i);
}
if (task2::check()) return task2::solve(), 0;
if (task4::check()) return task4::solve(), 0;
return 0;
}
Compilation message (stderr)
exam.cpp: In function 'bool task4::check()':
exam.cpp:55:22: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
55 | return (s.size() == n);
| ~~~~~~~~~^~~~
# | 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... |