#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <chrono>
#include <set>
#include <map>
#include <stack>
#include <functional>
#include <iomanip>
#include <queue>
#include <cassert>
#include <complex>
#include <cstring>
#include <memory>
#include <bitset>
#include <sstream>
#include <cmath>
#include <numeric>
#include <numbers>
#include <fstream>
using namespace std;
#ifndef template
#ifndef define
#define ll long long
#define ld long double
#define pl pair<ll, ll>
#define pi pair<int, int>
#define nl cout << '\n';
#define x first
#define y second
#define cbit(x) __builtin_popcountll(x)
#define uid(a, b) uniform_int_distribution<ll>(a, b)(rng)
#define siz(x) (int)x.size()
#endif
#ifndef print
void print(size_t x) {cout << x << ' ';}
void print(int x) {cout << x << ' ';}
void print(long long x) {cout << x << ' ';}
void print(float x) {cout << x << ' ';}
void print(long double x) {cout << x << ' ';}
void print(char x) {cout << x << ' ';}
void print(const char* x) {cout << x << ' ';}
void print(bool x) {cout << x << ' ';}
void print(string &x) {cout << x << ' ';}
template<typename T, typename V>
void print(pair<T, V> &p) {print(p.x); print(p.y);}
template<typename T>
void print(vector<T> v) {for (int i = 0; i < v.size(); i++) print(v[i]);}
template<typename T>
void print(vector<vector<T>> v) {
for (int i = 0; i < v.size(); i++){
for (int j = 0; j < v[i].size(); j++)
print(v[i][j]);
nl;
}
}
template <typename T, typename... V>
void print(T t, V&&... v) {print(t); print(v...);}
#endif
#ifndef read
void read(int &x) {cin >> x;}
void read(long long &x) {cin >> x;}
void read(unsigned &x) {cin >> x;}
void read(unsigned long long &x) {cin >> x;}
void read(float &x) {cin >> x;}
void read(long double &x) {cin >> x;}
void read(char &x) {cin >> x;}
void read(string &x) {cin >> x;}
void read(bool &x) {cin >> x;}
template<typename T>
void read(vector<T> &v) {
for (int i = 0; i < v.size(); i++)
read(v[i]);
}
template <typename T, typename... V>
void read(T &t, V&... v) {read(t); read(v...);}
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> bool maxi(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template<class T> bool mini(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
template<class... Args>
auto vec(size_t n, Args&&... args) {
if constexpr(sizeof...(args) == 1)
return vector(n, args...);
else
return vector(n, vec(args...));
}
#endif
using namespace std;
const ll inf = 1e15;
const ll def = 1e7+1;
const ll mod = 998244353;
/*
####%@@%#*%%#@#*###%++--#*******++**=++*++*+++=+===-==--=-=+===-::::::%#....*#*:..-..-
*##%%@@%###@@%=*#%**=+-:+*#++*++++++++++==========:..=-----==-::::....=#+...:=**=--...
*###%@###%@@%=:=****==--**+*++++++++==...........-=====-.:---::........+%:..:-:++*=:..
+*##%@###@@@++=+****=+-=+***++++=*+=....:::........-----=.:-::.........:##...:...=++=-
*%#%%%###%@*#*:-*#+***+****+=+*+=+..:=+**+=-.........::::-..:...........+#+..-:...==+*
+%###%%%%@+@%#:=**+=**+++++=-+===..=+#%%%#+++-::......::.:::.............**=:::..:..:=
+%#%#%%@%=%@*+==*=+=**+=+++======#####%@%@%=+*+:... ......:.............+*+=...:....
=#%#%%@%+%@@*:-++++=*+=======:=+##%@@%@%@@%%@@@@%#+++-........:............-**:.:.....
=####%*=*%@%#=-=**+-=+-=++=--==+##%@@%%@@@#+++=%+.:#*%......................-*#*-...:.
+*#%#++=*%@%+*-:+++-+=:-++++===:+##%%@@@@@@@%#+=: ......................=*#%+....
#%@#+##=+%@%+--++++=+=:-==+++=-.*@%%%@@@@@@@@%#=::+. . ........... ..........--.-#%#-.
@%*+##*-*#%#+:-=+++==-:==--===-:#@@@%@@@@@@@@%==:. .......................==:::=#%%
#####%+-**#*=-+=+++-+=:--:-==-::#%@@@@@@@@@@@%*#*+: ......................--.....:=
+##%%#=.**=++**++++-===------:.=%%@@@@@@@@@@@@%%%%#-=- ......................::.....::
####%*::***++***+++====+=---::.*%@@@@@@@@@@@@@@@@%%#+........................::......:
###*%+:-+*#********-=====---:::#%@@@@@@@@@@@@@@@@@#:.. .......................:......-
####*+=-++*+-+**+++-====-::::.=%%@@@@@@@@@@@@@@@@%=.............. ....... ....:......:
*##**=:++*##--++++=-----::-::.+%@@@@@@@@@@@@@@@@%+:.... ............. ......:.....:-
+****++++*%*:=++++==---:::::::*@@@@@@@@@@@@@@@@@@#=... ... ..............::.....:-
+***++*++*#*=+==-=-----::.. #@@@@@@@@@@@@@@@@%*=:... ...............-..::.::
*++++**+++==--=-------.::.. .%@%%%@@@@@@@@@@@%#++:... ..... ...........:.:::::-
*+*++++++======------=*#+==-:=@@*++##%@@@@@@%%###*-......=+++=.... ...........:.:::::-
#+*=+*++=====------.........=%@@=:-=+*#####++=*@#+:...... ..::...............:.::::*#
#**=+=++========-...........-@@@+:-=--=====-::=%%=--. .................+@@-+@
%#++*+======-==:........=*%%%*@@%--====----:.:=#%#::==-:.. ...........+%#-*@@+*
##++++++===---:::....-*#*+++*%@@@*=+===+==-::-+%@#%%##*++=:. . ........+%#=*@@-#@*
*===+==-:---=----:..+#==+++++-....... ..@@%%%#+++=..=.. ........+%#++%%*#@%%
=+=-----------=--::+#=--=+++:.......... ....=%%%%%##++-=-...:.. .......-#@@@#++%@*%@
*:+===-:..---===::=#+----=+=.........:......%%%%%##*==:=:....-.. ......-*=+:.#@-*@@=
---====------+===-+%----:-=-...::-::--:::..*%%%%%##+--.:......-. .....::::.::-::==-=
#*++===---::-++=+=*#------==::-===--*----:.%%%%%%#*=::........=. ......:...::::.:::=+
*+=++==---:-=+++++*#::::---=---==+++*=+=-::%%%%%%#*=:.........=: ....:::.==.::::-==..:
*+**=--:-=---*+*++*#::::-:-+--==++++#=+=--=%%%%%%#*=:... .....+. ....::.-**+----::-==-
##*#**++-:-==*+**+*%=::::-=*==+++***%**+=-=%%%%%%#*+-.... ..:+.....:-..=++-----===++=
##*****+++*+=+****+##:::::-#=++++***%**+===%%%%%%#*++-.... .+. .......::--------===-
%=......-=+=++*****+%+-::.:#++******%**++=+%%%%%%#**+=:..... *. . ....:---=--=#@@@@@
%#**###**=:-+-=*##*+#%=-::-**+******#***+=+%%%%%%%#*++=:.......+........---=+*%@%@@@@@
..+#%#####-..*+++****%+----+#+****#*##**+=+%%%%%%%%#*+=-:...............===+++++==*#@@
=.:%##*******.+*+=+**%=---:=#*+***#**%**+=*@@%%%%%%%#*+=-:.............:=++====+=+*==#
###*++***++==+*+====++--::--*%*#####*%**+=*@@@%%%%%%%##+==-:..........:==++=++=+++**##
%#%%#*-:-===--::-=-:---::-:--%%########**-..%@@%%%%%%%#*++==-:::::...-==+===-=**==#@@@
%#%%*::--====--::::::::::::...#%##%####**:....%@@@@@@%%%#+==--::::::---=--==-=-:=+*%@@
##%*##=-----:----::::::::::::.:+%%%%####*.......=@@@@@@@@@%%#***=:-::::::::-===-#@%%##
==+==+=++*++*+==++=====--=*-==--+%%%%#%#++=+=+++=++*@@@@@@@@@*=----:-::-=-+#@@%@@%#%%%
=====-:-+*#%%%%##%%@@%@@%%@@@@@@@@@@@@@@@@@@@@@@@%%%##*****+===------===**##%%@@@@@@@@
%#%%#***+=-=*%%%#%#*#@%%#+###*@@@@@@@%###@##**#+++++++-=====+==---==*##%%#****##%@@@@@
@%@@@%#+==++====++++++*##%#**++=++=====+++*===+*+++*++=-=---==**#%#%@@#+%@%###%%%%@@@@
chimpanzini bananini wah wah wah
--------------==========--------------===++++++++=======+++*******####****************##############
------------------------------------======++##======++++++++****************************************
----------------------------------==========**=+++++++++++++++++++++++++++++++++++++++**************
---------------------------------===========*##+=+++++++++++++++++++*+++++++++++++++++++++**********
------------------------------------=======++======================**=+++++++++++++++++++++++*******
--------------------------------------------===============================++++++++++++++++*********
------------------------------------------====================================++++++++++++++++******
------------------------------------------------=======-----====--====---=========+++++++++=++++++++
----------------------============================++++=--------==++++++*++++++++++++=++***#########*
-------========++++++++++******************+=---=+**+=--====+=**+**************#######%%%%%%%%%%%%%%
---------=========+++++=++++++++++++++++++=--=-=====++*************##*#**##########%%%%%%%%%%%%%%%%%
===--------------=============---===------=+**++++++**++++*******############%%%%%#%%##%%%%%%%%%%%%%
====++====-----=======+=++++++++++==-==++**+****+********#*****#########%%%%%%%%%%%%%%###%%%%%%%%%%%
=+=+==+++++=====--=++++++******+++==++************###########*####%%%%%%%%%%%%%%%%%%%%%%##%%%%%%%%%%
++++++++++++++++++===++++******++++++***##########%%%%%%%%%%####%%%%%%%%%%%%%%%%%%%%%##%%%%###%%%%%%
****+++++++++++++++++++++******+++****#####%%%%%%%###%%%%%%%##%%%%%%%%%%%%%%%%%%%%%%%%%%%###%%######
*******************++++++*****++****##%%%%%%%%#%%###%%%%%%%%%###%%%%%%%%%%%%%%%%#####%##############
###**#########********+++++++++**##%%%%%%%%%%#@%####%%%%%%%%%###########%%%%%##########***##*****+++
###*#######%%#####********+++++**##%%%%%%%%##@%%#####%################********+++++++++=*#*=========
#***#######%%%%%####***************+++***++#%@%%%######******+++++====++========--=====#%+==========
*****####################*+++====+++**######%@@@%%%%%=------===========+++++++++++++++%%*======+++++
********+++++++=====-----==+**#%%%%%%%%##%%%%%%%%%%##*=-----------------------------*%%+============
--------------------====*###%%@@@@@@@@@@@@%%%%%%%%%%%##*+=-------------------------#%%*--------=====
----------------+%%%@%%%%%@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##*=----===--------------=#%%*-----------===
-----------+#%%%%######%%%@%%%%%%%##%%%%%%###%%%%%%%%%%%%%##*+-------------===-==#%%#========-------
--------+####**#%**##%##%%%%#########%#%#####%%%%%%%%%%%%%%####+---------------=%%%%=---------------
----------=*+=+=+#+++++++++===++++++**=++**+*#%%%%%%#%%%%%%######+-----=*-----=%%%%+---------------=
---------------===++#%#*++++++++++++++++++++*#@@%%%%#%#%%%##########+*%#+----=@%%#+---------------=*
------------------==+++++++++++++++++++++++**%@@@%%%%%%##%%######%%##%%%###%%@%##*=---==-==++*#*+===
-----------------------==+++++++*++++******++++#%@%%%#++++**#%%#####%%####%##***#+==================
--------------=----------------=+++++++**********%@%###***++++*#%%%#******+******=-----=============
-------------------------------=*++++++++*********%@%###********#%%%#*+**+---=*#%+---------=========
----======-----------=======++==+++++=--=++++******%@%###******#####%%*=++-----=##=---------========
-===++*++++++++=======---------**+*+=-------------=+%%%%%*---+**=--+*###%%+-----------------========
=++==--------------------------##**=----------------#%@%%#+---==----*%%###*=-------------===========
----------------------------=+#%#*+==+---------------%%%%%*-------=+*######=--------=+==============
---------------------------=+#####*###+--------------+%%%##=------==*####**=-------==*##**++++++++++
-------------------------=+*###########---------------#%%#*=----+#######**#%###**######*+++++=======
-----------------------=+####+*#**#####------------+*#%%%#+-----=#%%%###%%%##########*++++==========
-------------------==+*########*#***+++=---------=######%%%%%#--=++**###############*===============
----------------#*##########****##%###%#*+=-----+########%%@%########################*+=============
----------------=+*##*****##%%%%%%%######*=---=*######################################+=============
-----------------+###%%#*++======+*########**######################%%%%################*+===========
-----------------------------------===++***##########*##%%**####%%%%%%%%%%%%###%###########*========
---------------------------------------=-=+#########*=**+*#%%#**%%%%%%%%%%%%%%%%%%%%#####*+=======++
--------------------------------------==+*###########=+*%#******%%%%%%%%%%%%%%%%%%%%%%%%%#*+====++++
==---==----------------------------++**##############%*******#@@%%%%%%%%%%%%%%%%%%%%%%%%%%%*+++=++++
==================--------------=%%#################****#%@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#*++++++
==================------=========+##*#######********#%@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##*
==========================+========#@@@@@@%%%%%%@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
==================================+#%@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
====================================+*#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#*###%%%
++==+++===================================+*#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##**++++**
+++++++++++++=++=========================+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#*********
+++++++++++++++=+++++=++++++++=======++++*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#********
tralalero tralala
*/
template<typename T>
struct SegmentTreeFast{
vector<T> a;
T defv;
int n;
SegmentTreeFast(int n, T defv) : n(n), defv(defv){
a = vector<T>(2 * n, defv);
}
T cmb(T a, T b){ //change if needed
return max(a, b);
}
void build(){ //array is at i + n index
for (int i = n - 1; i > 0; --i)
a[i] = cmb(a[i << 1], a[i << 1 | 1]);
}
void update(int i, T v){
T val = a[i + n];
for (a[i += n] = max(v, val); i > 1; i >>= 1)
a[i >> 1] = cmb(a[i], a[i ^ 1]);
}
T get(int l, int r){
r++;
T res = defv;
for (l += n, r += n; l < r; l >>= 1, r >>= 1){
if (l&1) res = cmb(res, a[l++]);
if (r&1) res = cmb(res, a[--r]);
}
return res;
}
};
int pos(vector<int> &v, int x){
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
void solve(){
int n, x;
read(n, x);
auto a = vec(n, 0);
read(a);
auto v = a;
sort(v.begin(), v.end());
auto dp1 = vec(n, 1);
SegmentTreeFast<int> st(n, 0);
for (int i = 0; i < n; i++){
int u = pos(v, a[i]);
if (u)
maxi(dp1[i], st.get(0, u - 1) + 1);
st.update(u, dp1[i]);
}
auto dp2 = vec(n, 0);
st = SegmentTreeFast<int>(n, 0);
int res = *max_element(dp1.begin(), dp1.end());
for (int i = n - 1; i >= 0; i--){
int u = pos(v, a[i]);
if (u + 1 < n)
maxi(dp2[i], st.get(u + 1, n - 1) + 1);
st.update(u, dp2[i]);
if (i > 0){
int u = pos(v, a[i - 1] - x);
if (u + 1 < n)
maxi(res, dp1[i - 1] + st.get(u + 1, n - 1));
}
}
print(res);
}
/*
*/
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (ifstream("input.txt").good()){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int t;
t = 1;
while (t--){
solve();
nl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
glo.cpp: In function 'int32_t main()':
glo.cpp:307:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
307 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:308:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
308 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |