| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1330014 | vuviet | Global Warming (CEOI18_glo) | C++20 | 211 ms | 8408 KiB |
/**
* Author: trviet
* Studies at: Le Hong Phong High School for the Gifted
**/
#include <bits/stdc++.h>
#define ____vuviet__ signed main
#define taskname "trviet"
using namespace std;
#define all(x) x.begin(), x.end()
constexpr int maxn = 2e5 + 5;
int n, x, t[maxn], f[maxn], g[maxn];
vector<int> values;
struct SegmentTree
{
int sz;
vector<int> st;
SegmentTree(int sz)
{
this->sz = sz;
st.resize(2 * sz + 5, 0);
}
void update(int i, int v)
{
st[i += sz] = v;
i /= 2;
while (i > 0) {
st[i] = max(st[i * 2], st[i * 2 + 1]), i /= 2;
}
}
int query(int l, int r)
{
int res = 0;
l += sz, r += sz + 1;
while (l < r)
{
if (l & 1) res = max(res, st[l++]);
if (r & 1) res = max(res, st[--r]);
l /= 2, r /= 2;
}
return res;
}
};
int idx(int v)
{
return lower_bound(all(values), v) - values.begin() + 1;
}
void solvebytrviet()
{
cin >> n >> x;
for (int i = 1; i <= n; ++i)
{
cin >> t[i], f[i] = g[i] = 1;
values.push_back(t[i]);
}
sort(all(values));
values.resize(unique(all(values)) - values.begin());
int m = values.size();
SegmentTree l(m), r(m), segtree(m);
for (int i = 1; i <= n; ++i)
{
f[i] = 1 + l.query(1, idx(t[i]) - 1);
l.update(idx(t[i]), f[i]);
}
for (int i = n; i >= 1; --i)
{
g[i] = 1 + r.query(idx(t[i]) + 1, m);
r.update(idx(t[i]), g[i]);
}
int ans = 0;
for (int i = n; i >= 1; --i)
{
int p = idx(t[i] - x + 1);
int best = (p <= m ? segtree.query(p, m) : 0);
ans = max(ans, f[i] + best);
segtree.update(idx(t[i]), g[i]);
}
cout << ans;
}
void freopen_stdio(string speech)
{
cin.tie(0)->sync_with_stdio(0);
cerr << speech << "\n";
if (fopen(taskname ".inp", "r"))
{
freopen(taskname ".inp", "r", stdin);
freopen(taskname ".out", "w", stdout);
}
}
____vuviet__()
{
freopen_stdio("!@#$%^&*()=_+");
int t = 1;
// cin >> t;
while (t-- > 0)
solvebytrviet();
return 0;
}
Compilation message (stderr)
| # | 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... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
