Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define OO INT_MAX
- const int N = 2e5 + 5e2;
- int Memo[N][2][2];
- vector< int > x;
- int n ,k ,y;
- /// ------------- Memo[i][F][S] ------------ ///
- /// --- i = Index -------------------------- ///
- /// --- F = If First Platform Used Or Not -- ///
- /// --- S = If Second Platform Used Or Not - ///
- int Calc(int i ,bool F ,bool S){
- if( i == n ) return 0;
- int & Re = Memo[i][F][S];
- if( Re + 1 ) return Re;
- Re = -OO;
- int EdP = x[i] + k; /// --- End Position --- //
- int newIdx = upper_bound(x.begin() ,x.end() ,EdP) - x.begin();
- Re = max(Re ,Calc(i + 1 ,F ,S)); // Try To Put Platform In i + 1
- if( !F ) Re = max(Re ,Calc(newIdx ,true ,S) + (newIdx - i) ); // Try Put F Platform
- if( !S ) Re = max(Re ,Calc(newIdx ,F ,true) + (newIdx - i) ); // Try Put S Platform
- return Re;
- }
- void Solve(){
- scanf("%d%d" ,&n ,&k); x.resize(n);
- for( auto &i : x ) scanf("%d" ,&i);
- for( auto i : x ) scanf("%d" ,&i);
- sort(x.begin() ,x.end());
- /// --- Memset(Dp = -1) --- ///
- for(int i = 0 ; i <= n ; i++)
- for(int F = 0 ; F < 2 ; F++)
- for(int S = 0 ; S < 2 ; S++)
- Memo[i][F][S] = -1;
- int Ans = Calc(0 ,false ,false);
- printf("%d\n" ,Ans);
- }
- int main() {
- int Tc; scanf("%d" ,&Tc);
- while( Tc-- ) Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement