#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5;
int n;
int a[NMAX + 5], b[NMAX + 5];
int dp[NMAX + 5];
/**
dp[i] = nr maxim pe care le putem face din primele i
=>
dp[i] = max{dp[j] + getCost(j, i) | 1 <= j <= i} =>
getCost putem calcula in O(N)
**/
int maxim[5005][5005];
int getCost (int x, int y)
{
int ret = 0;
for (int i=x; i<=y; i++)
ret += (b[i] == maxim[x][y]);
return ret;
}
int main()
{
cin >> n;
for (int i=1; i<=n; i++)
cin >> a[i];
for (int i=1; i<=n; i++)
cin >> b[i];
for (int i=1; i<=n; i++)
{
maxim[i][i] = a[i];
for (int j=i+1; j<=n; j++)
maxim[i][j] = max(maxim[i][j-1], a[j]);
}
for (int i=1; i<=n; i++)
{
dp[i] = getCost(1, i);
for (int j=2; j<=i; j++)
{
dp[i] = max(dp[i], dp[j-1] + getCost(j, i));
}
}
cout << dp[n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
6228 KB |
Output is correct |
2 |
Runtime error |
215 ms |
201524 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1060 ms |
67344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |