# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
463409 |
2021-08-11T06:05:16 Z |
amunduzbaev |
Exam (eJOI20_exam) |
C++14 |
|
39 ms |
1112 KB |
#include "bits/stdc++.h"
using namespace std;
//~ #define int long long
void solve1(int n, vector<int>& a, vector<int>& b){
vector<int> dp(n);
if(n <= 5005){
int res = 0;
for(int i=0;i<n;i++){
int s = i;
while(s + 1 < n && a[s + 1] <= a[i]) s++;
vector<int> cnt(n);
for(int j=0;j<n;j++){
if(j) cnt[j] = cnt[j-1];
cnt[j] += (b[j] == a[i]);
}
vector<int> pref(n);
for(int j=s;j>=0;j--){
if(a[j] > a[i]) break;
pref[j] = (j ? pref[j-1] : 0);
int cost = (j ? cnt[j-1] - dp[j-1] : 0);
pref[j] = min(pref[j], cost);
}
for(int r=s;r>=0;r--){
if(a[r] > a[i]) break;
dp[r] = max(dp[r], cnt[r] - pref[r]);
}
}
for(int i=0;i<n;i++) res = max(res, dp[i]);
cout<<res<<"\n";
} else {
int res = 0;
for(int i=0;i<n;i++){
int r = i, l = i;
while(r + 1 < n && a[r + 1] <= a[i]) r++;
while(l > 0 && a[l - 1] <= a[l]) l--;
vector<int> cnt(n);
for(int j=max(0, l-1);j<=r;j++){
if(b[j] == a[i]) cnt[j]++;
if(j + 1 < n) cnt[j+1] = cnt[j];
}
vector<int> pref(n);
for(int i=max(0, l-1);i<=r;i++){
pref[i] = max(pref[i], cnt[i] + dp[i]);
if(i + 1 < n) pref[i+1] = pref[i];
}
for(int j=l;j<=r;j++){
dp[j] = max(dp[j], cnt[j] + (j ? pref[j - 1] : 0));
}
}
for(int i=0;i<n;i++) res = max(res, dp[i]);
cout<<res<<"\n";
}
}
void solve2(int n, vector<int>& a, vector<int>& b){
int last = 0, cnt = 0, res = 0;
for(int i=0;i<n;i++){
if(a[i] > b[0]){
if(cnt){
res += (i - last);
}
cnt = 0, last = i + 1;
} else {
cnt |= (a[i] == b[i]);
}
} if(cnt){
res += (n - last);
}
cout<<res<<"\n";
}
void solve(){
int n; cin>>n;
vector<int> a(n), b(n);
for(auto& x : a) cin>>x;
for(auto& x : b) cin>>x;
int mx = 0, mn = 1e9;
for(auto x : b) mx = max(mx, x), mn = min(mn, x);
if(mx == mn){
solve2(n, a, b);
return;
}
if(n <= 5005){
solve1(n, a, b);
} else {
solve2(n, a, b);
}
}
/*
3
1 2 3
2 2 2
*/
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
//~ cin>>t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
5 ms |
460 KB |
Output is correct |
3 |
Correct |
19 ms |
1028 KB |
Output is correct |
4 |
Correct |
15 ms |
1100 KB |
Output is correct |
5 |
Correct |
30 ms |
1108 KB |
Output is correct |
6 |
Correct |
15 ms |
1112 KB |
Output is correct |
7 |
Correct |
16 ms |
1112 KB |
Output is correct |
8 |
Correct |
25 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |