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>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
class SegTree {
public:
vi tr; int n;
int l(int x) {return (x<<1);}
int r(int x) {return (x<<1)|1;}
int combine(int a, int b) {return max(a, b);}
void build(int i, int L, int R, vi& arr) {
if (L == R) tr[i] = arr[L];
else {
int mid = (L + R) / 2;
build(l(i), L, mid, arr);
build(r(i), mid + 1, R, arr);
tr[i] = combine(tr[l(i)], tr[r(i)]);
}
}
void build(vi &arr) {
build(1, 0, n - 1, arr);
}
SegTree(vi &arr) {
n = arr.size();
tr.resize(4*n);
this->build(arr);
}
int query(int i, int a, int b, int L, int R) {
if (L > b || R < a) return 0;
if (L >= a && R <= b) return tr[i];
int mid = (L + R) / 2;
return combine(
query(l(i), a, b, L, mid),
query(r(i), a, b, mid + 1, R));
}
int query(int i, int j) {
return query(1, i, j, 0, n - 1);
}
void update(int i, int a, int L, int R, int v) {
if (a < L || a > R) return;
if (L == R) {tr[i] = v; return;}
int mid = (L + R) / 2;
update(l(i), a, L, mid, v);
update(r(i), a, mid + 1, R, v);
tr[i] = combine(tr[l(i)], tr[r(i)]);
}
void update(int i, int v) {
update(1, i, 0, n - 1, v);
}
};
int n, x;
vi arr;
int test(vi&arr) {
vi vals(n);
map<int,int> cmpr;
rep(i,0,n) vals[i] = arr[i];
sort(vals.begin(), vals.end());
cmpr.insert({vals[0], 0}); int k = 1;
rep(i,0,n) {
if (vals[i] != vals[i - 1]) {cmpr.insert({vals[i], k});k++;}
}
vi trarr(n, 0);
SegTree st(trarr);
rep(i,0,n) {
int V = cmpr[arr[i]];
int ans = st.query(0, V - 1) + 1;
if (st.query(V, V) < ans) st.update(V, ans);
}
return st.query(0, n - 1);
}
int main()
{
cin >> n >> x;
arr.resize(n);
rep(i,0,n) cin>>arr[i];
if (n > 55) {
int ans = test(arr);
cout << ans;
} else {
int ans = 0;
rep(start, 0, n) {
rep(end, start, n) {
if (start == 0) {
rep(i,0,end+1)arr[i] -= x;
ans = max(ans, test(arr));
rep(i,0,end+1)arr[i] += x;
} else {
rep(vcd, 0, start) {
int change = arr[vcd] + 1 - arr[start];
if (abs(change) > x) continue;
rep(i,start,end+1) arr[i] += change;
ans = max(ans, test(arr));
rep(i,start,end+1) arr[i] -= change;
}
}
}
}
cout << ans;
}
}
# | 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... |