#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<typename... Args> // tuples
ostream& operator<<(ostream& os, tuple<Args...> t) {
apply([&](Args... args) { string dlm = "{"; ((os << dlm << args, dlm = ", "), ...); }, t);
return os << "}";
}
template<typename T, typename V> // pairs
ostream& operator<<(ostream& os, pair<T, V> p) { return os << "{" << p.f << ", " << p.s << "}"; }
template<class T, class = decltype(begin(declval<T>()))> // iterables
typename enable_if<!is_same<T, string>::value, ostream&>::type operator<<(ostream& os, const T& v) {
string dlm = "{";
for (auto& i : v) os << dlm << i, dlm = ", ";
return os << "}";
}
template <typename T, typename... V>
void printer(string pfx, const char *names, T&& head, V&& ...tail) {
int i = 0;
while (names[i] && names[i] != ',') i++;
constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
if (is_str) cerr << " " << head;
else cerr << pfx, cerr.write(names, i) << " = " << head;
if constexpr (sizeof...(tail)) printer(is_str ? "" : ",", names + i + 1, tail...);
else cerr << endl;
}
#ifdef LOCAL
#define dbg(...) printer(to_string(__LINE__) + ": ", #__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
tuple<int, vt<int>, vt<int>> read() {
int n; cin >> n;
vt<int> a(n), b(n), c;
F0R (i, n) cin >> a[i], c.pb(a[i]);
F0R (i, n) cin >> b[i], c.pb(b[i]);
sort(all(c));
c.erase(unique(all(c)), c.end());
auto ord = [&] (int x) { return lower_bound(all(c), x) - c.begin(); };
for (auto& x : a) x = ord(x);
for (auto& x : b) x = ord(x);
return {n, a, b};
}
/*
we need to figure out what final configurations of A are possible
a person's level can only increase
rules:
1. some dude's work will always be a continuous range
2. some dude's work can only spread up to the next larger guy on either side
3. left to right ordering must be the same
as long as final state assignments follow these, its possible to construct
subtask 2: B is equal
sorted stack to find next larger element on either side of each student with level B
sum ranges
subtask 4: elements of A are distinct
if kid i was to pass, then we know who gave him their work
let dp[i][j] be the max passes, only allowing the first i kids to spread their work, considering only the first j kids in the line
everything else: n <= 5000
let dp[i][j] be the max value of assigning some work to the first j kids, using only the first i kids' work
for each kid, they are allowed to "spread" their range up to the next larger element on either side
for each prefix, starting from the first kid he can claim, find the new max value of the prefix, additionally allowing the kid to claim some suffix of it
*/
int n;
vt<int> a, b;
void solve3() {
}
void solve2() {
}
void solve1() {
vt<vt<int>> dp(n + 1, vt<int>(n + 1)); // 1-indexed
vt<pi> rng(n); // reachable range
F0R (i, n) {
int j = i;
while (j < n && a[j] <= a[i]) j++;
rng[i].s = j - 1;
j = i;
while (j >= 0 && a[j] <= a[i]) j--;
rng[i].f = j + 1;
}
dbg(rng);
// dp
F0R (i, n) {
dbg(dp[i]);
F0R (j, n + 1) dp[i + 1][j] = dp[i][j];
auto [l, r] = rng[i];
int best = dp[i][l];
FOR (j, l, r + 1) {
best = max(best + (b[j] == a[i]), dp[i][j + 1]);
dp[i + 1][j + 1] = best;
}
}
cout << dp[n][n] << endl;
}
main() {
cin.tie(0)->sync_with_stdio(0);
tie(n, a, b) = read();
if (n <= 5000) {
solve1();
} else {
int v = b[0];
bool is_same = true;
FOR (i, 1, n) is_same &= b[i] == v;
if (is_same) solve2();
else solve3();
}
}
Compilation message
exam.cpp:134:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
134 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1232 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
13 ms |
16288 KB |
Output is correct |
4 |
Correct |
71 ms |
90916 KB |
Output is correct |
5 |
Correct |
77 ms |
98652 KB |
Output is correct |
6 |
Correct |
77 ms |
98900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
608 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
13 ms |
16288 KB |
Output is correct |
10 |
Correct |
71 ms |
90916 KB |
Output is correct |
11 |
Correct |
77 ms |
98652 KB |
Output is correct |
12 |
Correct |
77 ms |
98900 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
608 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
4452 KB |
Output is correct |
23 |
Correct |
96 ms |
98572 KB |
Output is correct |
24 |
Correct |
71 ms |
98648 KB |
Output is correct |
25 |
Correct |
58 ms |
98644 KB |
Output is correct |
26 |
Correct |
54 ms |
98632 KB |
Output is correct |
27 |
Correct |
57 ms |
98628 KB |
Output is correct |
28 |
Correct |
64 ms |
98896 KB |
Output is correct |
29 |
Correct |
68 ms |
98648 KB |
Output is correct |
30 |
Correct |
96 ms |
98644 KB |
Output is correct |
31 |
Correct |
71 ms |
98444 KB |
Output is correct |
32 |
Correct |
74 ms |
98652 KB |
Output is correct |