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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")
using namespace std;
using namespace __gnu_pbds;
using str = string;
using ll = long long;
using pii = pair <int,int>;
using pll = pair <ll,ll>;
using vi = vector <int>;
using vll = vector <ll>;
using vpii = vector <pii>;
using vpll = vector <pll>;
template<class A, class B>
ostream& operator<<(ostream& os, const pair<A, B> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &v) {
bool van = 1; os << '{';
for(auto &i : v) { if(!van) os << ", "; os << i; van = 0; }
os << '}'; return os;
}
template<class T, size_t sz>
ostream& operator<<(ostream&os, const array<T,sz> &arr) {
bool fs = 1; os << '{';
for(auto &i : arr) { if(!fs) os << ", "; os << i; fs = 0; }
os << '}'; return os;
}
#define mp make_pair
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define all(x) x.begin(), x.end()
#define watch(x) cerr << (#x) << " is : " << (x) << newl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
ll quickpow(ll num1, ll num2, const T MOD) {
assert(num2 >= 0); ll ans = 1;
for(; num2; num2>>=1, num1 = num1 * num1 % MOD) if(num2 & 1) ans = ans * num1 % MOD;
return ans;
}
// end of Template
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vector<int> a(n+1), b(n+1);
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) cin >> b[i];
const auto sub2 = [&b, &n]() -> bool {
for(int i = 2; i <= n; ++i) if(b[i] != b[i-1]) return false;
return true;
};
const auto sub4 = [&a, &n]() -> bool {
set<int> s;
for(int i = 1; i <= n; ++i) s.emplace(a[i]);
return s.size() == n;
};
if(sub2()) {
bool hasb = false;
int len = 0;
int res = 0;
for(int i = 1; i <= n; ++i) {
if(a[i] > b[i]) {
res += hasb * len;
len = 0;
hasb = false;
continue;
}
++len;
if(a[i] == b[i]) hasb = true;
}
res += hasb * len;
return cout << res << newl, 0;
}
if(sub4()) {
map<int,int> pos;
for(int i = 1; i <= n; ++i) pos[a[i]] = i;
class sparseTable {
private:
vector<vector<int>> st;
int n;
public:
sparseTable(vector<int> &v) : n(v.size() - 1) {
st.emplace_back(vector<int>(n+1));
for(int i = 1; i <= n; ++i) st[0][i] = v[i];
for(int i = 1; (1<<i) <= n; ++i) {
st.emplace_back(vector<int>(n+1));
for(int j = 1; j + (1<<i) - 1 <= n; ++j) {
st[i][j] = max(st[i-1][j], st[i-1][j+(1<<(i-1))]);
}
}
}
int getMin(int l, int r) {
int lv = __lg(r - l + 1);
return max(st[lv][l], st[lv][r-(1<<lv)+1]);
}
};
class fenwick {
private:
vector<int> BIT;
int n;
public:
fenwick(int n) : n(n) {
BIT.resize(n+1);
}
void update(int idx, int val) {
for(int i = idx; i <= n; i += i & -i) BIT[i] = max(BIT[i], val);
return;
}
int getMax(int idx) {
int res = 0;
for(int i = idx; i; i -= i & -i) res = max(res, BIT[i]);
return res;
}
};
sparseTable sparse(a);
fenwick BIT(n);
int ans = 0;
for(int i = 1; i <= n; ++i) {
if(!pos.count(b[i])) continue;
if(sparse.getMin(min(i, pos[b[i]]), max(i, pos[b[i]])) > b[i]) continue;
int res = 1 + BIT.getMax(pos[b[i]]);
BIT.update(pos[b[i]], res);
ans = max(ans, res);
}
return cout << ans << newl, 0;
}
vector<vector<bool>> can(n+1, vector<bool>(n+1));
for(int i = 1; i <= n; ++i) {
can[i][i] = true;
for(int mx = a[i], j = i - 1; j; --j) {
mx = max(mx, a[j]);
can[i][j] = (mx == a[j]);
}
for(int mx = a[i], j = i + 1; j <= n; ++j) {
mx = max(mx, a[j]);
can[i][j] = (mx == a[j]);
}
}
vector<vector<int>> memo(n+1, vector<int>(n+1, -1));
const auto dp = [&](const auto &self, int id1, int id2) -> int {
if(id1 < 1 || id2 < 1) return 0;
int &res = memo[id1][id2];
if(~res) return res;
res = 0;
if(b[id2] == a[id1] && can[id2][id1]) {
res = max(res, 1 + self(self, id1, id2 - 1));
}
else {
res = max(res, self(self, id1 - 1, id2));
res = max(res, self(self, id1, id2 - 1));
}
return res;
};
cout << dp(dp, n, n) << newl;
return 0;
}
Compilation message (stderr)
exam.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("O3")
|
exam.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
exam.cpp:8: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
8 | #pragma comment(linker, "/stack:200000000")
|
exam.cpp: In lambda function:
exam.cpp:84:25: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | 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... |