This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
map<int, int> mp; int rnk = 0;
int pref[5001][10000], dp[5001];
void solve() {
int n;
cin >> n;
set<int> st;
int a[n + 1], b[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i]; st.insert(a[i]);
} for (int i = 1; i <= n; i++) {
cin >> b[i]; st.insert(b[i]);
} for (int v : st) mp[v] = rnk++;
bool flg = 1;
for (int i = 2; i <= n; i++) {
if (b[i] != b[i - 1]) {
flg = 0; break;
}
} if (flg) {
int ans = 0, cur = 0; bool flg = 0;
for (int i = 1; i <= n; i++) {
cur = 0; flg = 0;
while (a[i] <= b[i] && i <= n) {
if (a[i] == b[i]) flg = 1;
cur++; i++;
} if (flg) ans += cur;
}
cout << ans;
return;
}
for (int i = 1; i <= n; i++) {
a[i] = mp[a[i]]; b[i] = mp[b[i]];
for (int j = 0; j < 10000; j++) {
pref[i][j] = pref[i - 1][j];
} pref[i][b[i]]++;
} for (int j = 1; j <= n; j++) {
int mx = -1;
for (int i = j; i >= 1; i--) {
mx = max(mx, a[i]);
dp[j] = max(dp[j], dp[i - 1] + pref[j][mx] - pref[i - 1][mx]);
}
} int mx = 0;
for (int i = 1; i <= n; i++)
cout << dp[i] << ' ';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
exam.cpp: In function 'void solve()':
exam.cpp:66:11: warning: unused variable 'mx' [-Wunused-variable]
66 | } int mx = 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... |