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 <bits/stdc++.h>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x) (((x) >> (i)) & 1)
#define TASK "financial"
using namespace std;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) a = b; else return false; return true;
}
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) a = b; else return false; return true;
}
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
int n;
int d;
int a[N];
namespace subtask2 {
int dp[405][405];
vector <int> value;
void solve() {
for (int i = 1; i <= n; ++i) value.push_back(a[i]);
sort(all(value));
value.erase(unique(all(value)), value.end());
int m = (int) value.size();
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(all(value), a[i]) - value.begin() + 1;
}
memset(dp, -0x3f, sizeof dp);
for (int i = 1; i <= n; ++i) dp[i][a[i]] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = i - 1; j >= max(i - d, 1); --j) {
for (int mx = 1; mx <= m; ++mx) {
maxi(dp[i][max(mx, a[i])], dp[j][mx] + (mx < a[i]));
}
}
}
cout << *max_element(dp[n] + 1, dp[n] + m + 1);
}
};
namespace subtask3 {
vector <int> value;
int dp[7005][7005];
deque <int> dq[7005];
void solve() {
for (int i = 1; i <= n; ++i) value.push_back(a[i]);
sort(all(value));
value.erase(unique(all(value)), value.end());
int m = (int) value.size();
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(all(value), a[i]) - value.begin() + 1;
}
memset(dp, -0x3f, sizeof dp);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
while (dq[j].size() && dq[j].front() + d < i) dq[j].pop_front();
if (dq[j].size()) maxi(dp[i][max(a[i], j)], dp[dq[j].front()][j] + (a[i] > j));
while (dq[max(j, a[i])].size() && dp[dq[max(j, a[i])].back()][max(j, a[i])] <= dp[i][j])
dq[max(j, a[i])].pop_back();
dq[max(a[i], j)].push_back(i);
}
maxi(dp[i][a[i]], 1);
dq[a[i]].push_back(i);
}
//for (int i = 1; i <= n; ++i)
cout << *max_element(dp[n] + 1, dp[n] + m + 1);
}
};
namespace subtask4 {
stack <int> st;
void solve() {
int ans = 0;
for (int i = n; i >= 1; --i) {
while (st.size() && st.top() <= a[i]) st.pop();
st.push(a[i]);
maxi(ans, st.size());
}
cout << ans;
}
};
namespace subtask5 {
void solve() {
vector <int> dp;
for (int i = 1; i <= n; ++i) {
int p = lower_bound(all(dp), a[i]) - dp.begin();
if (p == dp.size()) dp.push_back(a[i]);
else dp[p] = a[i];
}
cout << (int) dp.size() << '\n';
}
};
namespace subtask6 {
int dp[N];
int m[N];
int r[N];
pair <int, int> b[N];
struct IntervalTree {
int st[N << 2];
void init(int value) {
for (int i = 1; i <= n * 4; ++i) st[i] = value;
}
void update(int id, int l, int r, int i, int value) {
if (l > i || r < i) return;
if (l == r) {
st[id] = value;
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, i, value);
update(id << 1 | 1, mid + 1, r, i, value);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return -1e9;
if (u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int walk(int id, int l, int r, int u, int v, int value) {
if (l > v || r < u) return -1;
if (u <= l && r <= v) {
if (st[id] <= value) return -1;
while (l < r) {
int mid = l + r >> 1;
if (st[id << 1] > value) {
id = id << 1;
r = mid;
} else {
id = (id << 1) | 1;
l = mid + 1;
}
}
return l;
}
int mid = l + r >> 1;
int res = walk(id << 1, l, mid, u, v, value);
if (res == -1) return walk(id << 1 | 1, mid + 1, r, u, v, value);
return res;
}
};
void solve() {
IntervalTree IT1, IT2, IT3;
IT1.init(-1e9);
IT2.init(-1e9);
IT3.init(-1e9);
for (int i = 1; i <= n; ++i) IT1.update(1, 1, n, i, -a[i]);
for (int i = n; i - d + 1 >= 1; --i) {
m[i] = IT1.get(1, 1, n, i - d + 1, i);
IT2.update(1, 1, n, i, -m[i]);
}
for (int i = 1; i <= n; ++i) {
//cout << -m[i] << ' ';
}
//cout << '\n';
for (int i = n; i >= 1; --i) {
if (i + d - 1 <= n) {
int res = IT2.walk(1, 1, n, i + d - 1, n, a[i]);
//cout << res << " \n"[i == n];
r[i] = (res == -1 ? n : res);
} else {
r[i] = n;
}
//cout << IT2.get(1, 1, n, i, i) << ' ';
}
//cout << '\n';
for (int i = 1; i <= n; ++i) {
b[i] = make_pair(a[i], i);
//cout << r[i] << ' ';
}
//cout << '\n';
sort(b + 1, b + n + 1, [](pair <int, int> x, pair <int, int> y) {
return (x.fi > y.fi) || (x.fi == y.fi && x.se < y.se);
});
memset(dp, -0x3f, sizeof dp);
for (int i = 1; i <= n; ++i) {
int pos = b[i].se;
int cur = max(IT3.get(1, 1, n, pos + 1, r[pos]), 0) + 1;
dp[pos] = cur;
IT3.update(1, 1, n, pos, dp[pos]);
}
//for (int i = 1; i <= n; ++i) cout << dp[i] << ' ';
//cout << '\n';
cout << *max_element(dp, dp + n + 1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> d;
for (int i = 1; i <= n; ++i) cin >> a[i];
//if (n <= 400) subtask2::solve();
// if (n <= 7000) subtask3::solve();
// else if (d == 1) subtask4::solve();
// else if (d == n) subtask5::solve();
subtask6::solve();
}
Compilation message (stderr)
Main.cpp: In function 'void subtask5::solve()':
Main.cpp:120:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | if (p == dp.size()) dp.push_back(a[i]);
| ~~^~~~~~~~~~~~
Main.cpp: In member function 'void subtask6::IntervalTree::update(int, int, int, int, int)':
Main.cpp:149:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
149 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int subtask6::IntervalTree::get(int, int, int, int, int)':
Main.cpp:159:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
159 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'int subtask6::IntervalTree::walk(int, int, int, int, int, int)':
Main.cpp:169:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
169 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:182:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
182 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In instantiation of 'bool maxi(T1&, T2) [with T1 = int; T2 = long unsigned int]':
Main.cpp:108:32: required from here
Main.cpp:11:11: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
11 | if (a < b) a = b; else return false; return true;
| ~~^~~
# | 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... |