#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define all(a) a.begin(),a.end()
#define mod (ll)(10000007)
using namespace std;
const int mx = 1e6 + 15;
vector<int> a;
vector<int> b;
int solve(int s, int e){
if (e-s<=1) return 0;
map<int,int> mp;
int mid = (s+e)/2;
int mx = -1;
int cnt = 0;
for (int i = mid-1;i>=s;i--){
mp[b[i]]++;
if (b[i]<=mx && binary_search(a.begin()+mid,a.begin()+e,b[i])){
cnt+=mp[b[i]];
}
else if (mp[b[i]]>cnt&&binary_search(a.begin()+mid,a.begin()+e,b[i])){
cnt = mp[b[i]];
mx = b[i];
}
}
int p = s;
for (int i=mid;i<e;i++){
if (a[i]==mx){
p = i;
break;
}
}
return max(solve(s,mid)+solve(mid,e),cnt+solve(p,e));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int,int>> ab(n);
a.resize(n);
b.resize(n);
bool flag = 1;
bool ans = 0;
for (int i=0;i<n;i++){
cin >> ab[i].first;
a[i] = ab[i].first;
}
for (int i=0;i<n;i++){
cin >> ab[i].second;
b[i] = ab[i].second;
if (i){
if (ab[i-1].second!=ab[i].second) flag = 0;
}
}
for (int i=0;i<n;i++){
if (ab[i].first==ab[i].second) ans = 1;
}
if (flag&&ans) {
int cnt =0;
for (int i=0;i<n;i++){
if (ab[i].first==ab[i].second){
int p = i;
while (ab[p].first<=ab[i].second){
cnt++;
p--;
if (p==-1) break;
}
p = i+1;
if (p!=n){
while (ab[p].first<=ab[i].second){
cnt++;
p++;
if (p==n) break;
}
}
i = p-1;
}
}
cout << cnt ;
}
else {
cout << solve(0,n);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
338 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
15 ms |
2072 KB |
Output is correct |
4 |
Correct |
10 ms |
2144 KB |
Output is correct |
5 |
Correct |
31 ms |
2208 KB |
Output is correct |
6 |
Correct |
12 ms |
2120 KB |
Output is correct |
7 |
Correct |
11 ms |
2156 KB |
Output is correct |
8 |
Correct |
22 ms |
2356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
317 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1108 ms |
512864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
338 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
338 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |