#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb push_back
#define fi first
#define se second
#define vector2d(x) vector<vector<x>>
#define pii pair<int, int>
#define pl pair<ll, ll>
#define kagamine_len ios_base::sync_with_stdio(0); cin.tie(0);
#define file_in freopen("input.txt", "r", stdin);
#define file_out freopen("output.txt", "w", stdout);
#define all(x) x.begin(), x.end()
using namespace std;
/*
g++ -std=c++20 EJOI2020_Exam.cpp -o output/EJOI2020_Exam.exe
./output/EJOI2020_Exam.exe
Input
N -> banyak murid yg mengikuti ujian
A1 .. AN -> skor yg akan mrk dapat
B1 .. BN -> skor buat lulus hrs exactly Bi
dp[l][r][0] = jumlah maksimum murid yg lulus rentang nyontek beda
dp[l][r][1] = jumlah maksimum murid yg lulus rentang nyontek sama
for subtask 1, 5 (30 points)
tandain kl lebih besar dr anu gbs
abis itu tiap segment di-max-in aja
for subtask 2 (12 points) - secured
*/
void subtask_2(){
int n; cin >> n;
vector<ll> a(n+1), b(n+1), seg(n+2, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
vector<bool> mark(n+2, 0);
vector<int> idx;
ll ma = 0;
for (int i = 1; i <= n; i++){
if (a[i] > b[i]){
mark[i] = 1;
ma = 0;
idx.pb(i);
continue;
}
ma = max(a[i], ma);
seg[i] = ma;
}
idx.pb(n+1);
int bef = 0;
for (int j : idx){
ll mx = seg[j-1];
for (int i = j-1; i > bef; i--){
seg[i] = mx;
}
bef = j;
}
int ans = 0;
for (int i = 1; i <= n; i++){
if (seg[i] == b[i]) ans++;
// cout << seg[i] << " ";
}
// cout << "\n";
cout << ans << "\n";
}
void solve(){
int n; cin >> n;
vector<int> a(n+1), b(n+1);
int dp[n+1][n+1][2];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int len = 1; len <= n; len++){
for (int l = 1; (l+len-1) <= n; l++){
int r = l+len-1;
if (l == r){
dp[l][r][0] = dp[l][r][1] = (a[l] == b[l]);
continue;
}
for (int mid = l; mid < r; mid++){
int le = max(dp[l][mid][0], dp[l][mid][1]),
ri = max(dp[mid+1][r][0], dp[mid+1][r][1]);
dp[l][r][0] = max(dp[l][r][0], le+ri);
}
int ma = 0;
for (int i = l; i <= r; i++){
ma = max(a[i], ma);
}
for (int i = l; i <= r; i++){
if (ma == b[i]) dp[l][r][1]++;
}
}
}
int ans = max(dp[1][n][0], dp[1][n][1]);
cout << ans << "\n";
}
/*
14
1 2 6 4 1 1 5 7 4 9 2 4 3 2
4 4 4 4 4 4 4 4 4 4 4 4 4 4
should be 8
*/
int main(){
kagamine_len
int tc = 1;
// cin >> tc;
while (tc --> 0){
solve();
}
return 0;
}
| # | 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... |