#include <bits/stdc++.h>
using namespace std;
const int BIT = 17;
const int N = (1 << BIT);
const int INF = 1e9;
int n; int lt[N]; int rt[N];
int a[N]; int b[N]; int rmq[BIT][N];
set<int> v[2 * N];
int maks(int l, int r){
int x = log2(r - l + 1);
return max(rmq[x][l], rmq[x][r - (1 << x) + 1]);
}
void find_bound(){
for (int i = 0; i < n; ++i){
rmq[0][i] = a[i];
lt[i] = INF; rt[i] = INF;
}
for (int i = 1; i < BIT; ++i){
for (int j = 0; j <= n - (1 << i); ++j)
rmq[i][j] = max(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
for (int i = 0; i < n; ++i){
if (v[b[i]].empty()) continue;
if (a[i] == b[i]){
lt[i] = i; rt[i] = i; continue;
}
auto it = v[b[i]].lower_bound(i);
if (it != v[b[i]].end()){
int x = (*it);
if (maks(i, x) == b[i])
rt[i] = x;
}
if (it == v[b[i]].begin()) continue;
--it;
int x = (*it);
if (maks(x, i) == b[i])
lt[i] = x;
}
}
void compress(){
set<int> s; int cnt = 1;
map<int, int> mp;
for (int i = 0; i < n; ++i){
s.insert(a[i]); s.insert(b[i]);
}
for (int x: s){
mp[x] = cnt; ++cnt;
}
for (int i = 0; i < n; ++i){
a[i] = mp[a[i]]; b[i] = mp[b[i]];
}
}
bool cluster_2(){
for (int i = 1; i < n; ++i){
if (b[i] != b[i - 1]) return 0;
}
return 1;
}
bool cluster_4(){
set<int> s;
for (int i = 0; i < n; ++i)
s.insert(a[i]);
if (s.size() != n)
return 0;
return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
compress();
for (int i = 0; i < n; ++i)
v[a[i]].insert(i);
find_bound();
if (cluster_2()){
int bio[N] = { 0 };
queue<int> q; int ans = 0;
for (int i = 0; i < n; ++i){
if (a[i] == b[0]){
q.push(i); bio[i] = 1;
}
}
while (!q.empty()){
int x = q.front(); q.pop();
int dx[2] = {-1, 1};
for (int i = 0; i < 2; ++i){
int y = x + dx[i];
if (y >= 0 && y < n && !bio[y] && a[y] <= b[0]){
bio[y] = 1; q.push(y);
}
}
}
for (int i = 0; i < n; ++i)
ans += bio[i];
cout << ans;
}
else if (cluster_4()){
}
else{
int dp[2][N];
for (int i = 0; i < 2; ++i){
for (int j = 0; j <= n; ++j)
dp[i][j] = -INF;
}
dp[0][0] = 0; dp[1][0] = 0;
for (int i = 1; i <= n; ++i){
//[0] - ulijevo ide
if (lt[i - 1] != INF){
dp[0][i] = 1;
for (int j = 1; j < i; ++j){
if (a[j - 1] >= b[i - 1] || lt[i - 1] > j - 1)
dp[0][i] = max(dp[0][i], dp[0][j] + 1);
if (b[j - 1] < a[i - 1] && lt[i - 1] > j - 1)
dp[0][i] = max(dp[0][i], dp[1][j] + 1);
else if (b[j - 1] > a[i - 1] && rt[j - 1] < i - 1)
dp[0][i] = max(dp[0][i], dp[1][j] + 1);
else if (a[j - 1] == b[i - 1] && rt[j - 1] == i - 1)
dp[0][i] = max(dp[0][i], dp[1][j] + 1);
}
}
//[1] - udesno
if (rt[i - 1] != INF){
dp[1][i] = 1;
for (int j = 1; j < i; ++j){
dp[1][i] = max(dp[1][i], dp[0][j] + 1);
if (b[j - 1] <= a[i - 1] || rt[j - 1] < i - 1)
dp[1][i] = max(dp[1][i], dp[1][j] + 1);
}
}
}
int ans = 0;
for (int i = 0; i <= n; ++i)
ans = max(ans, max(dp[0][i], dp[1][i]));
cout << ans;
}
return 0;
}
Compilation message
exam.cpp: In function 'bool cluster_4()':
exam.cpp:71:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
71 | if (s.size() != n)
| ~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
13652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
13804 KB |
Output is correct |
2 |
Correct |
17 ms |
16384 KB |
Output is correct |
3 |
Correct |
76 ms |
30056 KB |
Output is correct |
4 |
Correct |
47 ms |
26424 KB |
Output is correct |
5 |
Correct |
148 ms |
32844 KB |
Output is correct |
6 |
Correct |
43 ms |
26660 KB |
Output is correct |
7 |
Correct |
54 ms |
26692 KB |
Output is correct |
8 |
Correct |
119 ms |
29248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
13668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
13652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
13652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |