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 ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
return (n |= 1 << k);
}
ll set_off(ll n, ll k){
return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
int bit = (n >> k) & 1U;
if(bit == 1)
return true;
return false;
}
const ll INF = 1e9;
int n;
ll x;
int tree[4 * 600010];
void update(int node, int l, int r, int pos, int val){
if(l == r && l == pos){
tree[node] = max(tree[node], val);
}
else{
int mid = (l + r) / 2;
if(pos <= mid)
update(node * 2, l, mid, pos, val);
else
update(node * 2 + 1, mid + 1, r, pos, val);
tree[node] = max(tree[node*2], tree[node * 2 + 1]);
}
}
int query(int node, int l, int r, int L, int R){
if(r < L || l > R) return 0;
if(L <= l && r <= R) return tree[node];
int mid = (l + r) / 2;
int left = query(node * 2, l, mid, L, R);
int right = query(node * 2 + 1, mid + 1, r, L, R);
return max(left, right);
}
void solve(){
cin>>n>>x;
vl arr(n);
set<ll> nums;
for(auto &s : arr)
{
cin>>s;
nums.insert(s);
nums.insert(max(s - x + 1, 0LL));
nums.insert(min(s + x - 1, INF));
}
map<int,int> compress, rev_compress;
int id = 1;
for(auto t : nums){
compress[t] = id;
rev_compress[id] = t;
id++;
}
int start[n], end[n];
for(int i = 0; i < n; i++){
start[i] = end[i] = 1;
}
vi d(n+1, INF);
vi pos(n+1, -1);
d[0] = -INF;
for(int i = 0; i < n; i++){
int l = upper_bound(d.begin(), d.end(), arr[i]) - d.begin();
if(d[l-1] < arr[i] && arr[i] < d[l]){
d[l] = arr[i];
end[i] = l;
}
}
for(int i = 0; i < arr.size(); i++)
arr[i] *= -1;
reverse(arr.begin(), arr.end());
d.resize(n+1, INF);
d[0] = -INF;
for(int i = 0; i < n; i++){
int l = upper_bound(d.begin(), d.end(), arr[i]) - d.begin();
if(d[l-1] < arr[i] && arr[i] < d[l]){
d[l] = arr[i];
start[i] = l;
}
}
reverse(arr.begin(),arr.end());
for(int i = 0, j = n - 1; i < j; i++, j--){
swap(start[i], start[j]);
}
for(int i = 0; i < arr.size(); i++)
arr[i] *= -1;
int ans = 0;
for(int i = 0; i < n; i++){
ans = max(ans, end[i]);
ans = max(ans, start[i]);
}
memset(tree, 0, sizeof tree);
for(int i = n - 1; i > 0; i--){
update(1, 0, id, compress[arr[i]], start[i]);
int left = compress[max(arr[i-1]-x + 1, 0LL)], right = compress[min(arr[i-1]+x-1, INF)];
int best = query(1, 0, id, left, right);
// debug(left, right, best, end[i-1], left, right);
ans = max(ans, best + end[i-1]);
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout<<fixed;
startTime = clock();
int t=1;
// cin>>t;
while(t--)
solve();
return 0;
}
Compilation message (stderr)
glo.cpp: In function 'void solve()':
glo.cpp:113:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int i = 0; i < arr.size(); i++)
| ~~^~~~~~~~~~~~
glo.cpp:129:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(int i = 0; i < arr.size(); i++)
| ~~^~~~~~~~~~~~
# | 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... |