#include<bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PB push_back
#define F first
#define S second
const int INF=1e9;
const long long LINF=1e18;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<int, int> mii;
void setIO()
{
FAST_IO;
}
void setIO (string s) {
setIO();
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
const int N=5e3+10, L=28;
int n, a[N], b[N], st[N][L], dp[N][N];
void build()
{
FOR(i, 1, L)
{
for(int j=1; j+(1<<i)-1<=n; j++){
st[j][i]=max(st[j][i-1], st[j+(1<<(i-1))][i-1]);
}
}
}
int get(int l, int r)
{
int ln=r-l+1;
int k=0;
while((1<<(k+1))<=ln){
k++;
}
return max(st[l][k], st[r-(1<<k)+1][k]);
}
int main()
{
setIO();
cin >> n;
FOR(i, 0, n)
{
cin >> a[i+1];
st[i+1][0]=a[i+1];
}
FOR(i, 0, n) cin >> b[i+1];
build();
FOR(i, 1, n+1)
{
FOR(j, 1, n+1)
{
if(a[i]==b[j]){
int mx=get(min(i, j), max(i, j));
if(mx==a[i]){
dp[i][j]=1;
dp[i][j]+=max(dp[i][j-1], dp[i-1][j-1]);
}
else{
dp[i][j]=max(max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
}
}
else{
dp[i][j]=max(max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
}
}
}
cout << dp[n][n];
}
Compilation message
exam.cpp: In function 'void setIO(std::string)':
exam.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exam.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8348 KB |
Output is correct |
2 |
Runtime error |
7 ms |
1868 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
3404 KB |
Output is correct |
3 |
Correct |
23 ms |
24264 KB |
Output is correct |
4 |
Correct |
89 ms |
94944 KB |
Output is correct |
5 |
Correct |
94 ms |
98944 KB |
Output is correct |
6 |
Correct |
95 ms |
98992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
98988 KB |
Output is correct |
2 |
Runtime error |
7 ms |
1860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
3404 KB |
Output is correct |
9 |
Correct |
23 ms |
24264 KB |
Output is correct |
10 |
Correct |
89 ms |
94944 KB |
Output is correct |
11 |
Correct |
94 ms |
98944 KB |
Output is correct |
12 |
Correct |
95 ms |
98992 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
2 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
1 ms |
1228 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
7 ms |
8396 KB |
Output is correct |
23 |
Correct |
331 ms |
99156 KB |
Output is correct |
24 |
Correct |
268 ms |
98984 KB |
Output is correct |
25 |
Correct |
128 ms |
99000 KB |
Output is correct |
26 |
Correct |
96 ms |
98884 KB |
Output is correct |
27 |
Correct |
120 ms |
98880 KB |
Output is correct |
28 |
Correct |
95 ms |
98932 KB |
Output is correct |
29 |
Correct |
95 ms |
98884 KB |
Output is correct |
30 |
Correct |
95 ms |
98976 KB |
Output is correct |
31 |
Correct |
93 ms |
98896 KB |
Output is correct |
32 |
Correct |
95 ms |
98920 KB |
Output is correct |